Note that in above program, we have used retainAll(Collection<?> c) API of ArrayList implementation class.

retainAll(Collection<?> c) Method

retainAll(Collection<?> c) - This method retains only the elements in this list that are contained in the specified collection (optional operation). In other words, removes from this list all of its elements that are not contained in the specified collection.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.