CopyOnWriteArrayList addAllAbsent method

CopyOnWriteArrayList provides a new method public int addAllAbsent(Collection<? extends E> c) , which adds all of the elements in the specified collection that are not already contained in this list, to the end of this list. This is helpful when you need to add only unique elements into your list.
addAllAbsent(Collection c) returns an int – the number of elements that are added from the given collection.

Sample Code

Output

In the code above, we added a new collection that included – “One”, “Five”, “Six” and “Zero” to the copyOnWriteArrayList. Since CopyOnWriteArrayList already had “Zero” and “One”, it did not add it again. Only the other elements were added.

CopyOnWriteArrayList addAllAbsent method

Reference

1. CopyOnWriteArrayList addAllAbsent method java docs

It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedIn