Advertisement
DulcetAirman

delete by predicate

Feb 5th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.59 KB | None | 0 0
  1. package ch.claude_martin;
  2.  
  3. import java.util.HashMap;
  4.  
  5. public class SomeClass {
  6.     public static void main(String[] args) {
  7.         HashMap<Integer, String> map = new HashMap<>();
  8.         map.put(1, "foo");
  9.         map.put(2, "bar");
  10.         map.put(3, "qux");
  11.         map.put(4, "grault");
  12.         map.put(5, "garply");
  13.         map.put(6, "thud");
  14.         map.put(7, "flob");
  15.         System.out.println(map);
  16.         // {1=foo, 2=bar, 3=qux, 4=grault, 5=garply, 6=thud, 7=flob}
  17.  
  18.         // delete by predicate
  19.         map.entrySet().removeIf(e -> e.getValue().startsWith("f"));
  20.  
  21.         System.out.println(map);
  22.         // {2=bar, 3=qux, 4=grault, 5=garply, 6=thud}
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement