Java To Find Common, Uncommon, Unique Elements In ArrayLists

Elements in ArrayList

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", "[email protected]"));
		add(new User("Michael", "[email protected]"));
		add(new User("John", "[email protected]"));
		add(new User("Roytuts", "[email protected]"));
		add(new User("Roy Tutorials", "[email protected]"));
	}
};

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

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", "[email protected]"));
		add(new User("Michael", "[email protected]"));
		add(new User("John", "[email protected]"));
		add(new User("Roytuts", "[email protected]"));
		add(new User("Roy Tutorials", "[email protected]"));
	}
};

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

So to find common, uncommon, unique elements from the two Lists, you have to use the same methods which I have 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 protected]]]
Uncommon elements in List One: [[name=Soumitra, [email protected]], [name=Michael, [email protected]], [name=John, [email protected]], [name=Roytuts, [email protected]]]
Uncommon elements in List Two: [[name=Jerome, [email protected]], [name=Ford, [email protected]], [name=Microservices, [email protected]]]
Unique elements in List One and List Two: [[name=Michael, [email protected]], [name=John, [email protected]], [name=Roy Tutorials, [email protected]], [name=Ford, [email protected]], [name=Microservices, [email protected]], [name=Jerome, [email protected]], [name=Soumitra, [email protected]], [name=Roytuts, [email protected]]]

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

Source Code

Download

Share

Related posts

No comments

Leave a comment