0 votes
1 view
in Java by (3.4k points)

List<Integer> list = Lists.newArrayList(1,2,3);

List<Integer> list2 = null;

//throws nullPointer

list.addAll(list2);

//check here    

if (list2!=null){

    list.addAll(list2);   

}

Is there a Java 8 way of doing it simple in one-line.

This is one I have. but I actually don't need the boolean created.

boolean added = list2!=null ? list1.addAll(list2) : false

1 Answer

0 votes
by (46.1k points)

For Java 8 you can try, 

Optional.ofNullable(list2).ifPresent(list::addAll);

But I prefer ternary expression. 

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...