How to check if a map contains an empty string value in scala?

I have a newbie question.

We can use containsto check if the card contains the specified key, like:

val map = Map("a"->"1", "b"->"")
map.contains("a")

But now, I want to check if the card contains an empty string, is there any method?

+3
source share
3 answers

Try

map.values.exists(_ == "")

Edit: I think the above is the clearest, but I cannot resist showing the other two.

map.exists(_._2 == "")

more compact, but you must remember that _2 is the value when you iterate over a map.

map.values.exists(""==)

, _ == "" "".equals _ ""== . ( - , equals , , ? , () .)

+11

:

map.values.toSet.contains("")

map find { case (a,b) => b == "" } isDefined
0

Or you can simply write:

map contains ""
-1
source

Source: https://habr.com/ru/post/1795238/


All Articles