Hi,
I made the solution in the was which is at the comments.
But I would like to make it in other way - like it this way.
____________________________
public static void removeItemFromMap(HashMap<String, Integer> map) {
// int level = 500;
Iterator<Map.Entry<String, Integer>> iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry<String, Integer> entry = iter.next();
// if (entry.getValue() < level) {
// iter.remove();
// }
map.entrySet().removeIf(pairs -> pairs.getValue() < 500);
}
}
________________________________________________________
especially using the sentence: map.entrySet().removeIf(pairs -> pairs.getValue() < 500);
but it doesn't work.
Can anyone make a suggestion where is the error?
Jacek
Why it doesn't work
Resolved
Comments (2)
- Popular
- New
- Old
You must be signed in to leave a comment
jajaceek
26 December 2019, 18:58
Thanks, I removed the while loop and it works. That great. Thanks.
0
Guadalupe Gagnon
23 December 2019, 19:10
The code shared here is using 2 techniques that are colliding with each other. The code illegally removes items from a collection that is being looped over. Go back and fix your code so that:
A) you use the proper method to remove an item from a collection that you are currently looping
or
B) remove the loop and use the removeif method
0