/**
* @author lautturi.com
* Java example: java remove elements of hashmap by certain condition
*/
import java.util.*;
public class Lautturi {
public static void main(String[] args) {
HashMap<Integer,Integer> map = new HashMap<>();
map.put(1,12);
map.put(2,31);
map.put(3,17);
map.put(4,9);
map.put(5,16);
map.put(6,13);
map.put(7,21);
map.put(8,7);
System.out.println(map);
// remove the element that is greater than or equal to 16
map.values().removeIf(value -> value >= 16);
System.out.println(map);
}
}
output:
{1=12, 2=31, 3=17, 4=9, 5=16, 6=13, 7=21, 8=7}
{1=12, 4=9, 6=13, 8=7}