I continue my series of entries about less known features of the List class and today I'm going to explain the uses of addAll(), retainAll(), removeAll() methods.
Assume that you have two Lists and you want to merge them somehow.

If you take a look at the illustration above you'll see that red, green part of the sets and two blue part of the sets (elements of the intersection are used 2 times each) are obtained after addAll().
If we don't want elements of the second list to be added to the end of the first list then we can use addAll() with an index argument.
Notice that
list1.addAll(list2)
and
list1.addAll(list1.size(), list2)
are the same thing and adding the list2 to the list1 does not effect the content of list2.
Now, it's the removeAll()'s turn. You can remove all the elements of a Collection from a List using this method.

If you look at the picture above you'll see that we took only the red part of list1 and removed the intersection from it.
The last method of my entry will be retainAll(). retainAll() retains only shared elements of list1 and list2.

If you take a look at the picture above you'll see that we retain only the intersection which is the blue part of list1. That's all for part 2 secrets of List class part 2.