Java find different elements in two lists

Suppose you have two lists: A and B, and you need to find the elements that exist in both lists.

You can do it by just invoking the method List.retainAll().

Example:

public static void main(String[] args) { List<Integer> numbersA = new ArrayList<>(); List<Integer> numbersB = new ArrayList<>(); numbersA.addAll(Arrays.asList(new Integer[] { 1, 3, 4, 7, 5, 2 })); numbersB.addAll(Arrays.asList(new Integer[] { 13, 32, 533, 3, 4, 2 })); System.out.println("A: " + numbersA); System.out.println("B: " + numbersB); List<Integer> numbersC = new ArrayList<>(); numbersC.addAll(numbersA); numbersC.retainAll(numbersB); System.out.println("List A : " + numbersA); System.out.println("List B : " + numbersB); System.out.println("Common elements between A and B: " + numbersC); }

Java find different elements in two lists
PDF - Download Java Language for free

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", "")); add(new User("Michael", "")); add(new User("John", "")); add(new User("Roytuts", "")); add(new User("Roy Tutorials", "")); } }; List<User> ListTwo = new ArrayList<>() { private static final long serialVersionUID = 1L; { add(new User("Roy Tutorials", "")); add(new User("Jerome", "")); add(new User("Ford", "")); add(new User("Microservices", "")); } };

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", "")); add(new User("Michael", "")); add(new User("John", "")); add(new User("Roytuts", "")); add(new User("Roy Tutorials", "")); } }; List<User> ListTwo = new ArrayList<>() { private static final long serialVersionUID = 1L; { add(new User("Roy Tutorials", "")); add(new User("Jerome", "")); add(new User("Ford", "")); add(new User("Microservices", "")); } };

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=]] Uncommon elements in List One: [[name=Soumitra, email=], [name=Michael, email=], [name=John, email=], [name=Roytuts, email=]] Uncommon elements in List Two: [[name=Jerome, email=], [name=Ford, email=], [name=Microservices, email=]] Unique elements in List One and List Two: [[name=Michael, email=], [name=John, email=], [name=Roy Tutorials, email=], [name=Ford, email=], [name=Microservices, email=], [name=Jerome, email=], [name=Soumitra, email=], [name=Roytuts, email=]]

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

Source Code

Download

Tagged: Java Coding

Learn to compare two arraylists in Java with simple examples. We will first test if two arraylists are equal or not. If both lists are not equal, we will find the difference between lists.

The difference in list is equals to another third list which contains either additional elements or missing elements.

Also learn to find common elements between two arraylists.

1. Compare two arraylists for equality

Java program to test if two given lists are equal. To test equality –

  • Sort both lists.
  • Compare both lists using equals() method.

List.equals() method return true if both elements are of same size and both contains same set of elements in exactly same order.

public class ArrayListExample { public static void main(String[] args) { ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f")); ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e")); Collections.sort(listOne); Collections.sort(listTwo); //Compare unequal lists example boolean isEqual = listOne.equals(listTwo); //false System.out.println(isEqual); //Compare equals lists example ArrayList<String> listThree = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f")); isEqual = listOne.equals(listThree); //true System.out.println(isEqual); } }

Program output.

false true

2. Compare two arraylists – find additional elements

If two arraylists are not equal and we want to find what are additional elements in first list in comparison to second list then we can use this method.

It uses removeAll() method which removes all elements of second list from first list. It leaves only additonal elements in first list.

public class ArrayListExample { public static void main(String[] args) { ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f")); ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e")); //remove all elements of second list listOne.removeAll(listTwo); System.out.println(listOne); } }

Program output.

[f]

3. Compare two arraylists – find missing elements

To get the missing elements in first list, which are present in second list, we can reverse the above example. Here we can remove all elements of first list from second list using removeAll() method.

public class ArrayListExample { public static void main(String[] args) { ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f")); ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e")); //remove all elements from second list listTwo.removeAll(listOne); System.out.println(listTwo); } }

Program output.

[e]

4. Compare two arraylists – find common elements

To find common elements in two arraylists, use List.retainAll() method. This method retains only the elements in this list that are contained in the specified arraylist passed as method argument.

public class ArrayListExample { public static void main(String[] args) { ArrayList<String> listOne = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "f")); ArrayList<String> listTwo = new ArrayList<>(Arrays.asList("a", "b", "c", "d", "e")); listOne.retainAll(listTwo); System.out.println(listOne); } }

Program output.

[a, b, c, d]

Above example will work good in Java 8 as well.

Happy Learning !!

Read More:

A Guide to Java ArrayList
ArrayList Java Docs

Let us know if you liked the post. That’s the only way we can improve.