List Extract Common Elements Java

List Extract Common Elements Java


It may be required sometimes to extract the common elements of two lists in Java. Given in simple terms in this tutorial List Extract Common Elements Java.
Following code on List Extract Common Elements Java illustrates.


List Extract Common Elements Java
Output screenshot on List Extract Common Elements Java

Here we use retainAll() method of Collection interface. Following is the method signature:

boolean retainAll(Collection col): Keeps only those elements in the current data structure that also exist in the data structure passed as parameter. Other way to say, removes all the elements in the current data structure that do not exist in the collection col, passed as parameter. Returns true if the operation is successful, else, false.

One more program is available where common elements of two lists are removed.

Leave a Reply

Your email address will not be published. Required fields are marked *