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


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


1. CopyOnWriteArrayList addAllAbsent method java docs