如何在流式传输(lambda)时删除HashMap的元素 [英] How to remove an element of a HashMap whilst streaming (lambda)

查看:723
本文介绍了如何在流式传输(lambda)时删除HashMap的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下情况需要从流中删除元素。

  map.entrySet()。stream()。filter(t  - > t.getValue() ))。 
forEach(t - > map.remove(t.getKey()));

在前Java 8代码中,一个将从迭代器中删除 - 处理这种情况的最佳方法是什么这里?

解决方案

  map.entrySet()。removeIf(entry  - > entry。 。的getValue()等于( 0)); 

您无法使用流,但您可以使用其他新方法。 / p>

编辑:更好:

  map.values()。removeAll (Collections.singleton( 0)); 


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()));

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.

EDIT: even better:

map.values().removeAll(Collections.singleton("0"));

这篇关于如何在流式传输(lambda)时删除HashMap的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆