java 如果不为空,则将值放入地图中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48116499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Put value into map if not null
提问by Zimbo Rodger
Does Java have something like
Java 有没有类似的东西
map.putIfValueNotNull(key, value)
So I can put value in the map only if it`s not null without explicit checking.
因此,只有在没有显式检查的情况下它不为空时,我才能将值放入地图中。
回答by HDCase
You could use computeIfAbsent()
function which checks if the key is already in the map, if it is there it doesn't do anything.
If the key is not present then then it checks if the calculated value is null and puts into the map it if it is not.
您可以使用computeIfAbsent()
函数来检查密钥是否已经在地图中,如果它在那里,它什么都不做。如果键不存在,则它检查计算出的值是否为空,如果不是,则将其放入映射中。
map.computeIfAbsent(key, val -> value);
Be careful, putIfAbsnet
puts null values into the map.
小心,putIfAbsnet
将空值放入地图中。
回答by oleg.cherednik
I know that org.apache.commons.collections4.MapUtils
contains method safeAddToMap()
, but if value
is null
, it adds empty string
which is not what you want.
我知道org.apache.commons.collections4.MapUtils
包含方法safeAddToMap()
,但如果value
是null
,它会添加empty string
哪个不是你想要的。
I do not like variant, to override HashMap
to implement new method, because in this case you do not have it for other implementations: TreeMap
or LinkedHashMap
or else.
我不喜欢变型,覆盖HashMap
实现新的方法,因为在这种情况下,你不用它于其他实现方式:TreeMap
或LinkedHashMap
否则后果不堪设想。
I do not know about that this function exists in some availabe library, like Apache
or similar.
我不知道这个函数存在于一些可用的库中,比如Apache
或类似的。
Therefore, in my projects, I have to implement it myself:
因此,在我的项目中,我必须自己实现:
public static <K, V> Map<K, V> addNotNull(Map<K, V> map, K key, V value) {
if(value != null)
map.put(key, value);
return map;
}
回答by gaoagong
I tried the following and it seems to work.
我尝试了以下方法,它似乎有效。
Map<String, String> map = new HashMap<>();
String object1 = "blah";
String object2 = null;
map.compute("key1", (k, v) -> object1);
map.compute("key2", (k, v) -> object2);
System.out.println(map);
I don't know if there are performance or other considerations that would make this way undesirable. It looks pretty clean to me. If object1
or object2
is null, then they don't end up in the map. When I run this I get the following result.
我不知道是否有性能或其他考虑因素会使这种方式不受欢迎。对我来说看起来很干净。如果object1
或object2
为空,则它们不会出现在地图中。当我运行它时,我得到以下结果。
{key1=blah}
Note that if a value with the same key is already in the map, it will be replaced.
请注意,如果映射中已经存在具有相同键的值,则它将被替换。
回答by Neetesh Dadwariya
As per the java documentation, If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given mapping function and enters it into this map unless null.
根据 java文档,如果指定的键尚未与值关联(或映射为 null),则尝试使用给定的映射函数计算其值并将其输入此映射,除非为 null。
A simple example to put:
一个简单的例子:
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("1", "ONE");
map.put("2", null);
map.computeIfAbsent("3", val -> "THREE");
map.computeIfAbsent("4", val -> null);
System.out.println(map);
}
The output of this would be -
这个的输出将是 -
{1=ONE, 2=null, 3=THREE}
It is clear that the output does not contain the null values if added via computeIfAbsent method, while they are present if added by normal put method.
很明显,如果通过 computeIfAbsent 方法添加,则输出不包含空值,而如果通过普通 put 方法添加,则它们存在。