How to find Common, Uncommon, Unique Elements in Two ArrayLists using Java

Here I am going to show you how to find common, uncommon, unique elements in two Lists or ArrayLists using Java program. In this program I will show you how to find common, uncommon, unique string elements as well as object elements in two ArrayLists.

For identifying the unique string elements is very straight forward but for objects you need to override the equals() and hashCode() methods in your object class otherwise you won’t be able to find. When you are overriding equals() and hashCode() methods then you should consider those fields or attributes which are needed to make your object unique.

Prerequisites

Java

Common, Uncommon, Unique Elements

First I am going to show you how to find common, uncommon, unique elements in two Lists (ArrayLists) of String elements.

Let’s assume you have the following two list of strings.

List<User> ListOne = new ArrayList<>() {
	private static final long serialVersionUID = 1L;
	{
		add(new User("Soumitra", "Soumitra@email.com"));
		add(new User("Michael", "Michael@email.com"));
		add(new User("John", "John@email.com"));
		add(new User("Roytuts", "Roytuts@email.com"));
		add(new User("Roy Tutorials", "Roy-Tutorials@email.com"));
	}
};

List<User> ListTwo = new ArrayList<>() {
	private static final long serialVersionUID = 1L;
	{
		add(new User("Roy Tutorials", "Roy-Tutorials@email.com"));
		add(new User("Jerome", "Jerome@email.com"));
		add(new User("Ford", "Ford@email.com"));
		add(new User("Microservices", "Microservices@email.com"));
	}
};

Find common elements in both lists, you can use retainAll() method in the following way:

List<String> baseList = new ArrayList<>(ListOne);
baseList.retainAll(ListTwo);

The above code snippets will give you the following output:

Roy Tutorials

To find uncommon elements in list one (which has to substract list two) use the following code:

baseList = new ArrayList<>(ListOne);
baseList.removeAll(ListTwo);

The above code snippets will give you the following output:

Soumitra, Michael, John, Roytuts

To find uncommon elements in list two (which has to substract list one) use the following code:

baseList = new ArrayList<>(ListTwo);
baseList.removeAll(ListOne);

The above code snippets will give you the following output:

Jerome, Ford, Microservices

To find unique elements use the following code snippets:

Set<String> uniqueStrings = new HashSet<>();
uniqueStrings.addAll(ListOne);
uniqueStrings.addAll(ListTwo);

The above code snippets will give you the following output:

Soumitra, Roytuts, Roy Tutorials, Michael, John, Ford, Jerome, Microservices

Now I am going to show you how to perform the similar operations on objects. Let’s consider the following two ArrayLists of objects:

List<User> ListOne = new ArrayList<>() {
	private static final long serialVersionUID = 1L;
	{
		add(new User("Soumitra", "Soumitra@email.com"));
		add(new User("Michael", "Michael@email.com"));
		add(new User("John", "John@email.com"));
		add(new User("Roytuts", "Roytuts@email.com"));
		add(new User("Roy Tutorials", "Roy-Tutorials@email.com"));
	}
};

List<User> ListTwo = new ArrayList<>() {
	private static final long serialVersionUID = 1L;
	{
		add(new User("Roy Tutorials", "Roy-Tutorials@email.com"));
		add(new User("Jerome", "Jerome@email.com"));
		add(new User("Ford", "Ford@email.com"));
		add(new User("Microservices", "Microservices@email.com"));
	}
};

So to find common, uncommon, unique elements from the two Lists, you have to use the same methods which you used in the previous example on strings.

So you will see the following output when you apply those methods.

Common elements in List One and Two: [[name=Roy Tutorials, email=Roy-Tutorials@email.com]]
Uncommon elements in List One: [[name=Soumitra, email=Soumitra@email.com], [name=Michael, email=Michael@email.com], [name=John, email=John@email.com], [name=Roytuts, email=Roytuts@email.com]]
Uncommon elements in List Two: [[name=Jerome, email=Jerome@email.com], [name=Ford, email=Ford@email.com], [name=Microservices, email=Microservices@email.com]]
Unique elements in List One and List Two: [[name=Michael, email=Michael@email.com], [name=John, email=John@email.com], [name=Roy Tutorials, email=Roy-Tutorials@email.com], [name=Ford, email=Ford@email.com], [name=Microservices, email=Microservices@email.com], [name=Jerome, email=Jerome@email.com], [name=Soumitra, email=Soumitra@email.com], [name=Roytuts, email=Roytuts@email.com]]

The complete source code you can find from the below Source Code section for download.

Source Code

Download

Leave a Reply

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