Tuesday, August 18, 2015

Map & Lambda Expression

Problem

Given a HashMap<Character, Integer> map in Java 8, how would you remove the elements whose key > 's' or how would you remove elements with  value > 1 (in a single line)?

Solution

  map.entrySet().removeIf(p->p.getKey()>'s');
  map.entrySet().removeIf(p->p.getValue()>1);

No comments:

Post a Comment