Finding the Unique value of two ArrayLists in Java

1. Overview

Today we are going to discuss how to find differences between collections of objects of the string data type. Let’s take as an example, suppose we have a list of employees who applied for an interview, and another list of employees who already pass the interview. The difference between those two lists would give us the employee who didn’t pass the interview. We will take the ArrayList java example and I will show you code related to this example.

In Java, there is no option to find the differences between two lists in the List API in an explicit way, So if we want to find the difference between those then we need to use some helper methods.

In this article, we’ll learn how to Find the Unique value of Two ArrayList Java in different ways.

What is ArrayList and what user of Arraylist

ArrayList:

  • The underlying data structure for ArrayList and Vector is a resizable array growable array.
  • Duplicate objects are allowed in ArrayList and insertion order is preserved.
  • In ArrayList heterogeneous objects are allowed.
  • null Insertion is Possible in ArrayList.

 

Note: Except TreeSet and TreeMap everywhere heterogeneous objects are allowed.

 

ArrayList classes implement the RandomAccess interface. So that we can access any random element with the Same Speed.

For more information related to list and collection click

2. Code setup

Let’s take an example of two lists, which we’ll use to test our code

List listOne = Arrays.asList("Jack", "Tom", "Sam", "John", "James", "Rupesh");
List listTwo = Arrays.asList("Jack", "Daniel", "Sam", "Alan", "James", "George");

Requirement 

We need to find the unique value of listOne 

3. Code

package practice.String;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FindUniqueValue {

public static void main(String[] args) {
List listOne = Arrays.asList("Suraj", "Rakesh", "Anil", "John", "James", "Rupesh");
List listTwo = Arrays.asList("Suraj", "Pushpa", "Sam", "Alan", "James", "George");

List<String> differences = new ArrayList<>(listOne);
differences.removeAll(listTwo);
System.out.println("list of unique value of listtwo::"+differences);

}

}

Output:

list of unique value of listtwo::[Rakesh, Anil, John, Rupesh]

Requirement 

We need to find the unique value of listTwo 

package practice.String;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class FindUniqueValue {

public static void main(String[] args) {
List listOne = Arrays.asList("Suraj", "Rakesh", "Anil", "John", "James", "Rupesh");
List listTwo = Arrays.asList("Suraj", "Pushpa", "Sam", "Alan", "James", "George");

List<String> differences = new ArrayList<>(listTwo);
differences.removeAll(listOne);
System.out.println("list of unique value of listtwo::"+differences);

}

}

Output:

list of unique value of listtwo::[Pushpa, Sam, Alan, George]


If you want to do some other approch or other list compare then click 

Read more topics related to java

 

Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.

 

Leave a Reply

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