Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any null safe alternative to ArrayList.addAll?

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.

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?

like image 719
ZeDonDino Avatar asked Jun 09 '15 17:06

ZeDonDino


People also ask

Can null be stored to ArrayList?

In ArrayList, any number of null elements can be stored.

What does ArrayList addAll do?

addAll. Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator.

What does addAll () do in Java?

The addAll() method returns true if the collection changes as a result of elements being added into it; otherwise, it will return false .

What is the difference between ADD and addAll in ArrayList?

add is used when you need to add single element into collection. addAll is used when you want to add all elements from source collection to your collection. In this particular case you're using ArrayList without specifying a generic argument, so ArrayList<Object> is assumed.


2 Answers

In Java 8 Use below code:-

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.

like image 122
AlphaBetaGamma Avatar answered Sep 19 '22 14:09

AlphaBetaGamma


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 
like image 30
Jesper Avatar answered Sep 16 '22 14:09

Jesper