Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove multiple keys from a Map dynamically

Tags:

scala

Say I have a map like this:

scala> val a = Map("a"->2, "d" -> 4, "r" -> 5)
a: scala.collection.immutable.Map[String,Int] = Map(a -> 2, d -> 4, r -> 5)

I want to remove multiple keys dynamically from this. While I am able to do this:

scala> a - ("a","r")
res13: scala.collection.immutable.Map[String,Int] = Map(d -> 4)

But following fails:

scala> val keys = ("d","r")
as: (String, String) = (d,r)

scala> a - keys
<console>:15: error: type mismatch;
 found   : (String, String)
 required: String
       a - keys
           ^

While I understand this will be some casting issue, but I am not able to figure this simple thing out.

Edit

I also tried to make keys as array, as in earlier example it became a Tuple, but that also fails.

scala> val keys = Array("d","r")
as: Array[String] = Array(d, r)

scala> a - keys
<console>:15: error: type mismatch;
 found   : Array[String]
 required: String
       a - keys
           ^
like image 374
Saurabh Avatar asked Aug 17 '17 17:08

Saurabh


People also ask

How do I get rid of multiple keys on a map?

Assuming your set contains the strings you want to remove, you can use the keySet method and map. keySet(). removeAll(keySet); . keySet returns a Set view of the keys contained in this map.

How to remove list of keys from Map in Java 8?

Using removeAll() method A plausible way in Java 8 and above is using the removeAll() method, which removes all mappings associated with the specified keys. That's all about removing a key from a Map in Java.

How to remove items from Map in Java?

HashMap remove() Method in Java HashMap. remove() is an inbuilt method of HashMap class and is used to remove the mapping of any particular key from the map. It basically removes the values for any particular key in the Map. Parameters: The method takes one parameter key whose mapping is to be removed from the Map.

How do I delete a map key?

To delete a key from a map, we can use Go's built-in delete function. It should be noted that when we delete a key from a map, its value will also be deleted as the key-value pair is like a single entity when it comes to maps in Go.


2 Answers

To remove multiple keys from a Map, use the -- method:

a -- Set("a", "r")

The following explains the type mismatch error.

The version of the - method on Map that you're calling takes three arguments: (1) the key of the first element to remove, (2) the key of the second element to remove, and (3) a varargs representating zero or more remaining elements to remove. (The other version of - takes a single key as the argument.)

a - ("a", "r")

The above code is not passing a tuple to the - method; it's passing two String arguments to the - method. In other words, the above is equivalent to:

a.-("a", "r")

However, the code below...

val keys = ("d", "r")
a - keys

...is trying to pass a tuple as an argument to the - method. It's equivalent to:

a.-(("d", "r"))

You get a type mismatch error when you try to pass a tuple to the - method, because the - method expects one or more Strings.

like image 128
Jeffrey Chung Avatar answered Sep 29 '22 18:09

Jeffrey Chung


This is nice and concise.

scala> a - "a" - "r"
res0: scala.collection.immutable.Map[String,Int] = Map(d -> 4)

If you'd like to compose the list of keys before the removal.

val ks = Seq("d","r")
ks.foldLeft(a)(_ - _)  //res2: collection.immutable.Map[String,Int] = Map(a -> 2)
like image 41
jwvh Avatar answered Sep 29 '22 19:09

jwvh