Compare Three List in Java Example

Arraylist/List comparison in Java:
Now we have a scenario to split three list/arraylist values to seven lists with respect to list/arraylist values.

Input:

List 1 contains:
Java
PHP
26
God

List 2 contains:
C
Java
PHP

List 3 contains:
26
Guru
C 
Java

Expected Output:

only in list 1 ::: God
only in list 2 ::: 
only in list 3 ::: Guru
In 1 and 2 ::: PHP
In 1 and 3 ::: 26
In 2 and 3 ::: C
common in all lists ::: Java

Three List Comparison Java Source code:

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



public class ListCompare {
public static void main(String[] args){
	List<String> myList1 = new ArrayList<String>();
	myList1.add("Java");
	myList1.add("PHP");
	myList1.add("26");
	myList1.add("God");
	List<String> myList2 = new ArrayList<String>();
	myList2.add("C");
	myList2.add("Java");
	myList2.add("PHP");
	List<String> myList3 = new ArrayList<String>();
	myList3.add("26");
	myList3.add("Guru");
	myList3.add("C");
	myList3.add("Java");
	
	List<String> commonList = new ArrayList<String>();
	List<String> commonInList1and2 = new ArrayList<String>();
	List<String> commonInList1and3 = new ArrayList<String>();
	List<String> commonInList2and3 = new ArrayList<String>();
	
	for(String list2Val : myList2){
		for(String list1Val : myList1){
			for (String list3Val : myList3) {
				if(list3Val.equalsIgnoreCase(list1Val) && list3Val.equalsIgnoreCase(list2Val)){
					commonList.add(list3Val);
				}
			}
			
		}
	}
	
	
	myList3.removeAll(commonList);
	myList2.removeAll(commonList);
	myList1.removeAll(commonList);
	
	commonInList1and2 = twoListCompare(myList1,myList2);
	commonInList1and3 = twoListCompare(myList1,myList3);
	commonInList2and3 = twoListCompare(myList2,myList3);

	
	myList3.removeAll(commonInList2and3);
	myList3.removeAll(commonInList1and3);
	
	
	
	myList2.removeAll(commonInList2and3);
	myList2.removeAll(commonInList1and2);
	
	myList1.removeAll(commonInList1and3);
	myList1.removeAll(commonInList1and2);
	
	for (String string : myList1) {
		System.out.println("only in list 1 :::"+string);
	}
	
	for (String string : myList2) {
		System.out.println("only in list 2 :::"+string);
	}
	
	for (String string : myList3) {
		System.out.println("only in list 3 :::"+string);
	}
	
	for (String string : commonList) {
		System.out.println("common in all lists :::"+string);
	}
	
	for (String string : commonInList1and2) {
		System.out.println("In 1 and 2 :::"+string);
	}

	for (String string : commonInList1and3) {
		System.out.println("In 1 and 3 :::"+string);
	}
	for (String string : commonInList2and3) {
		System.out.println("In 2 and 3 :::"+string);
	}
}

private static List<String> twoListCompare(List<String> myList1,List<String> myList2){
	List<String> commonList = new ArrayList<String>();
	for(String list2Val : myList2){
		for(String list1Val : myList1){
			if(list2Val.equalsIgnoreCase(list1Val)){
				commonList.add(list2Val);
			}
		}
	}	
	return commonList;
}
}

Output:

only in list 1 :::God
only in list 3 :::Guru
common in all lists :::Java
In 1 and 2 :::PHP
In 1 and 3 :::26
In 2 and 3 :::C

Java Recommended Books:

333 total views, 1 views today

Leave a Reply

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