Retains only the elements in this list that are contained in the specified collection.
Retains only the elements in this list that are contained in the specified collection.In other words, removes from this list all of its elements that are not contained in the specified collection.
This implementation iterates over this list, checking each element returned by the iterator in turn to see if it's contained in the specified collection.
If it's not so contained, it's removed from this list with the iterator's remove method in case of a linked list and with an optimized direct removal method in case of an array list.
This method has a time complexity of O(N * M), with N being the size of the list and M being the size of the collection.