I was trying to use the shorthand method of using a for loop instead of the Iterator Class although I can't seem to get it working.
Is there something wrong with my logic or do I have to use an Iterator when removing items from a map?
Thanks in Advance!
package com.codegym.task.task08.task0818;
import java.util.HashMap;
import java.util.Map;
/*
Only for the rich
*/
public class Solution {
public static HashMap<String, Integer> createMap() {
HashMap<String, Integer> map = new HashMap<>();
map.put("Alan", 600);
map.put("David", 1000);
map.put("terry", 300);
map.put("derrek", 200);
map.put("john", 700);
map.put("derek", 200);
map.put("sam", 500);
map.put("tom", 30000);
map.put("matt", 900);
map.put("scott", 400);
return map;
}
public static void removeItemFromMap(HashMap<String, Integer> map) {
for(HashMap.Entry<String, Integer> pair : map.entrySet()){
if(pair.getValue() < 500){
map.remove(pair);
}
}
}
public static void main(String[] args) {
HashMap<String, Integer> map2 = createMap();
removeItemFromMap(map2);
}
}