Hi guys, I'm a bit confused, when I iterate over a map, I understand that I can remove directly from that map, without a copy of them map.. but with by code, it doesn't seem to work - do I need a copy? or am I doing some other thing wrong ? :-)
package com.codegym.task.task08.task0818;
import java.util.HashMap;
import java.util.Iterator;
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("Tranberg1", 100000);
map.put("Tranberg2", 100);
map.put("Tranberg3", 200);
map.put("Tranberg4", 2000);
map.put("Tranberg5", 100);
map.put("Tranberg6", 90);
map.put("Tranberg7", 80);
map.put("Tranberg8", 200);
map.put("Tranberg9", 100);
map.put("Tranberg10", 3000);
return map;
}
public static void removeItemFromMap(HashMap<String, Integer> map) {
Iterator<Map.Entry<String, Integer>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, Integer> pair = iterator.next();
if (pair.getValue() < 100) {
map.remove(pair.getKey());
}
}
}
public static void main(String[] args) {
removeItemFromMap(createMap());
}
}