CopyOnWriteArrayList addIfAbsent method

CopyOnWriteArrayList provides a new method public boolean addIfAbsent(E e), which adds elements only if that element is not present in the List.
This is helpful when you need to add only unique elements into your list.

addIfAbsent(E e) method return true if the element is added in the list, else returns false if the element already exists in the list.

Sample Code

Output

CopyOnWriteArrayList addIfAbsent method

Reference

1. CopyOnWriteArrayList addIfAbsent method java docs