Can someone please give an example of how to use the HashMap forall() method? I find the Scala docs to be impenetrable.
What I want is something like this:
val myMap = HashMap[Int, Int](1 -> 10, 2 -> 20)
val areAllValuesTenTimesTheKey = myMap.forall((k, v) => k * 10 == v)
but this gives:
error: wrong number of parameters; expected = 1
You need instead
The problem is that forall wants a function that takes a single
Tuple2, rather than two arguments. (We’re thinking of aMap[A,B]as anIterable[(A,B)]when we useforall.) Using a case statement is a nice workaround; it’s really using pattern matching here to break apart theTuple2and give the parts names.If you don’t want to use pattern matching, you could have also written
but I think that’s less helpful.