I have an Array of [Map[String,Int] like this:
val orArray = Array(Map("x" -> 24, "y" -> 25, "z" -> 26), null, Map("x" -> 11, "y" -> 22, "z" -> 33), null, Map("x" -> 111, "y" -> 222, "z" -> 333))
I want to remove the null elements in this array, to get something like:
Array[Map[String,Int]] = (Map("x" -> 24, "y" -> 25, "z" -> 26), Map("x" -> 11, "y" -> 22, "z" -> 33), Map("x" -> 111, "y" -> 222, "z" -> 333))
I was trying this so far
orArray.filterNot(p => p.isEmpty)
But it generates a NullPointerException. How could I filter out those two null values?
You can simply check the null values as
orArray.filter(map => map != null)
Output:
Map(x -> 24, y -> 25, z -> 26), Map(x -> 11, y -> 22, z -> 33), Map(x -> 111, y -> 222, z -> 333)
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With