Java 8, compare two maps and return result

  • A+
Category:Languages

I am comparing 2 maps using Java 8 features and based on condition wants to return the result. Using .forEach is showing compile time error and basically, the return is returning from Lambda expression and not from the loop. How can I return from the loop enclosing the lambda?
Note that I am not comparing for equality for the two map objects

nMap.forEach((k,v) -> {     if (!mMap.containsKey(k) || mMap.get(k) < v) {         return -1;     } }); 

 


Use a Stream of the entrySet() and anyMatch instead of forEach:

boolean found =      nMap.entrySet()         .stream()         .anyMatch(e -> !mMap.containsKey(e.getKey()) || mMap.get(e.getKey()) < e.getValue()); if (found)     return -1; 

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: