Please tell me there is a map

Map<Integer, Integer> denominations=new HashMap<>(); 

How can I use Stream API to remove all pairs whose value is zero?

    1 answer 1

     denominations = denominations.entrySet() .stream() .filter(e -> e.getValue() != 0) .collect(Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue)); 

    but it can be much easier without streams

     denominations.entrySet() .removeIf(e -> e.getValue() == 0); 
    • Displays type mismatch. With return does not want to work. - user281932
    • one
      The second option can be written as .values().removeIf( ((Integer)0)::equals ); . In short, more mysterious and will not fall, if the map is null . - zRrr