Dan*_*Dan 45 java dictionary iterator java-stream
我有以下情况需要从流中删除元素.
map.entrySet().stream().filter(t -> t.getValue().equals("0")).
forEach(t -> map.remove(t.getKey()));
Run Code Online (Sandbox Code Playgroud)
在Java 8之前的代码中,可以从迭代器中删除 - 在这里处理这种情况的最佳方法是什么?
Lou*_*man 103
map.entrySet().removeIf(entry -> entry.getValue().equals("0"));
Run Code Online (Sandbox Code Playgroud)
你不能用流做,但你可以用其他新方法做到这一点.
编辑:更好:
map.values().removeAll(Collections.singleton("0"));
Run Code Online (Sandbox Code Playgroud)
小智 7
如果您想删除整个密钥,请使用:
myMap.entrySet().removeIf(map -> map.getValue().containsValue("0"));
Run Code Online (Sandbox Code Playgroud)