如何过滤“空”来自HashMap的值< String,String>? [英] How to filter "Null" values from HashMap<String, String>?
本文介绍了如何过滤“空”来自HashMap的值< String,String>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在map之后,将键值对作为String,写一个逻辑来过滤Map中的所有空值而不使用任何外部API?
Following map, having both key-value pair as String, Write a logic to filter all the null values from Map without using any external API's ?
有没有除了遍历整个地图并过滤掉值之外的其他方法(遍历整个地图并获取条目对象并丢弃这些对)?
Is there any other approach than traversing through whole map and filtering out the values (Traversing whole map and getting Entry Object and discarding those pair) ?
Map<String,String> map = new HashMap<String,String>();
map.put("1", "One");
map.put("2", "Two");
map.put("3", null);
map.put("4", "Four");
map.put("5", null);
//Logic to filer values
//Post filtering It should print only ( 1,2 & 4 pair )
推荐答案
您可以使用Java 8方法 Collection.removeIf
用于此目的:
You can use the Java 8 method Collection.removeIf
for this purpose:
map.values().removeIf(Objects::isNull);
这删除了所有空值。
这篇关于如何过滤“空”来自HashMap的值< String,String>?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文