Java Tool To Compare Two Lists and Spit Out The Differences – Working Example

As a heavy java programmer, most of the time I encounter a problem where I have two lists and need to find out the differences between these two. One of the ways I sometimes quickly find out the difference is:

  • To print out the list and then use microsoft excel to compare the two columns.
  • Upload the file to temporary database tables and the run SQL query to find out the difference

As I started encountering the list compare more often, I thought of writing a tool that takes two lists and then simply print out the differences. Thus I coded the following simple piece of java program to achieve this.

The following program:

  • Can take an input of two ArrayLists presumably containing Strings, numbers of mix of both
  • It then compares the two lists and then prints out all items from first list which are not in the second lists and also prints out the items in second lists which are not in the first list

I can now use this program as often as I want and solves the hassle of me having to upload the data to database for compare or even import to excel to do the same.

Most of the programs from my blog come from my real world working experience. They are simple programs but sometimes save your huge time. Feel free to copy and modify any of the programs for your own use. Everything is open sourced and free in my blog

package com.kushal.tools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author Kushal Paudyal Last Modified On 2011-09-16
 * 
 *         This simple utility compares two String or number lists or a list
 *         that contains either strings or number and then prints out a list of
 *         items that are in first list but not in the second list and also a
 *         list of items that are in second list but not in the first list.
 */
public class ListCompare {

	public static void main(String args[]) {
		List listA = new ArrayList();
		listA.add(1);
		listA.add(2);
		listA.add("Kushal");
		listA.add("Madan");
		listA.add("Pooja");
		listA.add("Kripa");

		List listB = new ArrayList();
		listB.add(2);
		listB.add(3);
		listB.add("Kushal");
		listB.add("Madan");
		listB.add("Jenny");
		listB.add("Betsy");

		ListCompare listComp = new ListCompare();
		listComp.compareLists(listA, listB);

	}

	public void compareLists(List firstList, List secondList) {
		Map mapForFirstList = new HashMap();
		Map mapForSecondList = new HashMap();
		Iterator firstListIterator = firstList.iterator();
		Iterator secondListIterator = secondList.iterator();
		while (firstListIterator.hasNext()) {
			String firstListKeyValue = firstListIterator.next().toString();
			/**
			 * Put the value from the list into the map, only if the same value
			 * already does not exists. That means if there are duplicates, we
			 * put only one instance into the hashmap.
			 */
			if (!mapForFirstList.containsKey(firstListKeyValue)) {
				mapForFirstList.put(firstListKeyValue, firstListKeyValue);
			}

		}

		while (secondListIterator.hasNext()) {
			String secondListKeyValue = secondListIterator.next().toString();
			/**
			 * Put the value from the list into the map, only if the same value
			 * already does not exists. That means if there are duplicates, we
			 * put only one instance into the hashmap.
			 */
			if (!mapForSecondList.containsKey(secondListKeyValue)) {
				mapForSecondList.put(secondListKeyValue, secondListKeyValue);
			}

		}
		compareAndPrintResults(mapForFirstList, mapForSecondList);

	}

	private void compareAndPrintResults(Map mapForFirstList,
			Map mapForSecondList) {
		/** Compare first map against the second one and print the difference **/
		printItemsFromFirstListThatAreNotOnSecondList(mapForFirstList,
				mapForSecondList);
		/** Compare second map against the first and print the difference */
		printItemsFromSecondListThatAreNotOnFirstList(mapForSecondList,
				mapForFirstList);

	}

	private void printItemsFromFirstListThatAreNotOnSecondList(Map mapA,
			Map mapB) {
		System.out
				.println("***Items from first list that are not in second list");
		doComparisionAndPrint(mapA, mapB);

	}

	private void printItemsFromSecondListThatAreNotOnFirstList(Map mapA,
			Map mapB) {
		System.out
				.println("***Items from second list that are not in firstList list");
		doComparisionAndPrint(mapA, mapB);

	}

	/**
	 * @param mapA
	 * @param mapB
	 * 
	 *            This method compares two hashmaps and prints out the values
	 *            from the first one that are not in the second one.
	 */
	private void doComparisionAndPrint(Map mapA, Map mapB) {
		// both maps should be non-empty for comparison.
		if (mapA != null && mapB != null) {
			Iterator mapAIterator = mapA.keySet().iterator();

			while (mapAIterator.hasNext()) {
				String key = mapAIterator.next().toString();
				if (!mapB.containsKey(key)) {
					System.out.println(key);
				}
			}
		}
	}
}

The following is the output of this program:

		***Items from first list that are not in second list
		1
		Kripa
		Pooja
		***Items from second list that are not in firstList list
		3
		Betsy
		Jenny