本文介紹了如何在流式傳輸時刪除 HashMap 的元素(lambda)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有以下需要從流中刪除元素的情況.
I have the following situation where I need to remove an element from a stream.
map.entrySet().stream().filter(t -> t.getValue().equals("0")).
forEach(t -> map.remove(t.getKey()));
在 Java 8 之前的代碼中,可以從迭代器中刪除 - 在這里處理這種情況的最佳方法是什么?
in pre Java 8 code one would remove from the iterator - what's the best way to deal with this situation here?
推薦答案
map.entrySet().removeIf(entry -> entry.getValue().equals("0"));
你不能用流來做到這一點,但你可以用其他新方法做到這一點.
You can't do it with streams, but you can do it with the other new methods.
更好:
map.values().removeAll(Collections.singleton("0"));
這篇關(guān)于如何在流式傳輸時刪除 HashMap 的元素(lambda)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!