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

Replacing non-ASCII characters using Java Regular Expressions

The following java code shows how we can replace non-ASCII characters from a file using regular expressions.

package com.kushal.utils;

/**
 * ReplaceNonASCIICharacters.java
 * @author Kushal Paudyal
 * www.sanjaal.com/java
 *
 * This class reads a file with non ASCII Characters in it.
 * Replaces the non ASCII Characters using regular expression.
 * Saves the content with non-ASCII Characters removed to a new file.
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class ReplaceNonAsciiCharacters {

	public static void main(String args[]) {

		/**
		 * This is the input file name with some non-ASCII characters in the
		 * content of the file.
		 */
		String fileName = "C:/Temp/WithNonASCIICharacters.txt";
		/**
		 * This is the location of the output file - the content of this file
		 * will be the input file content minus the non-ASCII characters.
		 */
		String outputFileName = "C:/Temp/WithNonASCIICharactersRemoved.txt";

		try {

			/**
			 * Create a reader to read the input file
			 */
			BufferedReader in = new BufferedReader(new FileReader(fileName));
			String line = "";

			String formattedStr = "";
			int count = 0;
			/**
			 * Iterate through each line of content
			 * remove any non-ASCII characters with blank using
			 * regular expression.
			 *
			 * Append the new line character properly.
			 */
			while ((line = in.readLine()) != null) {
				if (count == 0)
					formattedStr += line.replaceAll("[^\p{ASCII}]", "");
				else
					formattedStr += "n" + line.replaceAll("[^\p{ASCII}]", "");

				count++;
			}

			/**
			 * Write the content to the output file using BufferedWriter object.
			 */
			BufferedWriter out = new BufferedWriter(new FileWriter(
					outputFileName));
			out.write(formattedStr);

			/**
			 * Once done, flush the writer and close it.
			 */
			out.flush();
			out.close();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	/*
	 * SANJAAL CORPS MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
	 * SUITABILITY OF THE SOFTWARE, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
	 * LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
	 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SANJAAL CORPS SHALL NOT BE
	 * LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING,
	 * MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
	 *
	 * THIS SOFTWARE IS NOT DESIGNED OR INTENDED FOR USE OR RESALE AS ON-LINE
	 * CONTROL EQUIPMENT IN HAZARDOUS ENVIRONMENTS REQUIRING FAIL-SAFE
	 * PERFORMANCE, SUCH AS IN THE OPERATION OF NUCLEAR FACILITIES, AIRCRAFT
	 * NAVIGATION OR COMMUNICATION SYSTEMS, AIR TRAFFIC CONTROL, DIRECT LIFE
	 * SUPPORT MACHINES, OR WEAPONS SYSTEMS, IN WHICH THE FAILURE OF THE
	 * SOFTWARE COULD LEAD DIRECTLY TO DEATH, PERSONAL INJURY, OR SEVERE
	 * PHYSICAL OR ENVIRONMENTAL DAMAGE ("HIGH RISK ACTIVITIES"). SANJAAL CORPS
	 * SPECIFICALLY DISCLAIMS ANY EXPRESS OR IMPLIED WARRANTY OF FITNESS FOR
	 * HIGH RISK ACTIVITIES.
	 */
}

Calculating Folder Size In Java

The following piece of java code demonstrates how we can calculate the size of a given folder using Java Application. This uses a recursive technique. We start with a root directory, and start listing the files/folder. If a file is found, we calculate its size and sum of total size and also increase the total number of files counted so far, if it is a directory, we further do the recursion.

This program has been fully compiled and tested to find the folder size in Java.

/**
 * @author Kushal Paudyal
 * www.sanjaal.com/java
 * Last Modified On: 30th August 2008
 */
package com.kushal.utilities;

import java.io.File;
import java.text.DecimalFormat;
import java.text.NumberFormat;

public class TopLevelFolderSize {

	int totalFolderCount=0;
	int totalFileCount=0;

	public static void main(String args [])
	{
		/**
		 * Define your folder here. This is the folder whose size
		 * statistics you want to find out.
		 */
		String folder = "C:/Users/kushal/Desktop/tempKushal";

		try{
			DecimalFormat fmt =new DecimalFormat("#.##");

			TopLevelFolderSize fg=new TopLevelFolderSize();

			/**Calculating the file size. By default size in long
			 * is returned.
			 */
			long fileSizeByte=fg.getFileSize(new File(folder));

			/**
			 * Formatting the long value to calculate size in
			 * different units KB, MB and GB
			 */
			double fileSizeKB=Double.valueOf(fmt.format(fileSizeByte /1024));
			double fileSizeMB=Double.valueOf(fmt.format(fileSizeByte /(1024*1024)));
			double fileSizeGB=Double.valueOf(fmt.format(fileSizeByte /(1024*1024*1024)));

			/**Printing the statistics**/
			System.out.println("nn##############--Folder Statistics--#################");
			System.out.println("Total Folder Size: ["+fileSizeByte+" Bytes] ntt["
						+fileSizeKB+" KB] ntt["
						+fileSizeMB+" MB] ntt["
						+fileSizeGB+" GB]");
			System.out.println("Total Number of Folders: "+fg.getTotalFolderCount());
			System.out.println("Total Number of Files: "+fg.getTotalFileCount());
			System.out.println("##########--End Of Folder Statistics--##############");

		}catch (Exception e)
		{
			System.out.println("Exception Occurred: "+e.getMessage());
		}
	}

	/**
	 * This is a recursive method.
	 * If file is found, total file size is calculated.
	 * If it is a folder, we recurse further.
	 */
	public long getFileSize(File folder) {
		totalFolderCount++; //Counting the total folders

		System.out.println("Processing " + folder.getName());
		long foldersize = 0;

		File[] filelist = folder.listFiles();
		for (int i = 0; i < filelist.length; i++) {
			if (filelist[i].isDirectory()) {
				foldersize += getFileSize(filelist[i]);
			} else {
				totalFileCount++; //Counting the total files
				foldersize += filelist[i].length();
			}
		}

		return foldersize;
	}

	/**
	 * @return the totalFolderCount
	 */
	public int getTotalFolderCount() {
		return totalFolderCount;
	}

	/**
	 * @return the totalFileCount
	 */
	public int getTotalFileCount() {
		return totalFileCount;
	}

}


———————————————————————
SAMPLE OUTPUT:
———————————————————————
Processing tempKushal
Processing cute pictures
Processing image col
Processing Moline Tornado
Processing send
Processing Party At Prabhakars
Processing images

##############–Folder Statistics–#################
Total Folder Size: [151850658 Bytes]
[148291.0 KB]
[144.0 MB]
[0.0 GB]
Total Number of Folders: 7
Total Number of Files: 154
##########–End Of Folder Statistics–##############