Java ArrayList.addAll 的任何空安全替代方案?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30739105/
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
Any null safe alternative to ArrayList.addAll?
提问by ZeDonDino
I was refactoring some old code of mine that I've written and I stumbeled on this code:
我正在重构我编写的一些旧代码,并且偶然发现了这段代码:
List<OcmImageData> fullImagePool = new ArrayList<>();
if (CollectionUtils.isNotEmpty(style.getTestMH())) {
fullImagePool.addAll(style.getTestMH());
}
if (CollectionUtils.isNotEmpty(style.getTrousers())) {
fullImagePool.addAll(style.getTrousers());
}
if (CollectionUtils.isNotEmpty(style.getDetailRevers())) {
fullImagePool.addAll(style.getDetailRevers());
}
if (CollectionUtils.isNotEmpty(style.getDetailCuffs())) {
fullImagePool.addAll(style.getDetailCuffs());
}
if (CollectionUtils.isNotEmpty(style.getDetailInner())) {
fullImagePool.addAll(style.getDetailInner());
}
if (CollectionUtils.isNotEmpty(style.getDetailMaterial())) {
fullImagePool.addAll(style.getDetailMaterial());
}
if (CollectionUtils.isNotEmpty(style.getComposing())) {
fullImagePool.addAll(style.getComposing());
}
...
So basically I need to create an ArrayList which contains all Lists here referenced, because those can be null (they are fetched out of the database from an closed sourced framework, and unfortunately its null if he doesn't find anything), I need to check everytime if the collection is not null to add them into this pool which looks just weird.
所以基本上我需要创建一个包含这里引用的所有列表的 ArrayList,因为这些列表可以为空(它们是从封闭源代码框架中从数据库中提取的,不幸的是,如果他没有找到任何东西,则它为空),我需要每次检查集合是否为空以将它们添加到这个看起来很奇怪的池中。
Is there a library or Collection-Framework utility class that gives me the posibility to add a collection to another without performing the null-safe check?
是否有一个库或 Collection-Framework 实用程序类可以让我在不执行空安全检查的情况下将一个集合添加到另一个集合?
回答by djechlin
This refactors cleanly to
这干净地重构为
for (OcmImageData elem : new List<OcmImageData>[] { style.getTestMH(), style.getTrousers() /* etc */}) {
if (CollectionUtils.isNotEmpty(elem)) {
fullImagePull.addAll(elem);
}
}
To answer your original question, no, you will have to do your own null check. You can see Guava's methods will throw an NPE, and Apache's methods explicitly require the input to be not null.
要回答您最初的问题,不,您必须自己进行空检查。您可以看到Guava 的方法会抛出 NPE,而 Apache 的方法明确要求输入不为 null。
回答by Jesper
Just write a small utility method:
只需编写一个小实用方法:
public static <E> void addAllIfNotNull(List<E> list, Collection<? extends E> c) {
if (c != null) {
list.addAll(c);
}
}
so that you can write:
这样你就可以写:
List<OcmImageData> fullImagePool = new ArrayList<>();
addAllIfNotNull(fullImagePool, style.getTestMH());
addAllIfNotNull(fullImagePool, style.getTrousers());
addAllIfNotNull(fullImagePool, style.getDetailRevers());
// ...etc
回答by Jean Logeart
Using Java 8:
使用 Java 8:
List<OcmImageData> fullImagePool = Stream.of(style.getTestMH(), /* etc */)
.filter(Objects::nonNull)
.flatMap(l -> l.stream())
.collect(Collectors.toList());
回答by AlphaBetaGamma
In Java 8 Use below code:-
在 Java 8 中使用以下代码:-
Optional.ofNullable(listToBeAdded).ifPresent(listToBeAddedTo::addAll)
listToBeAdded - The list whose elements are to be added. listToBeAddedTo - The list to which you are adding elements using addAll.
listToBeAdded - 要添加其元素的列表。listToBeAddedTo - 您使用 addAll 添加元素的列表。