Java: Laugh or Cry, Your Choice...

Posted on October 31, 2007

Sometimes I’ll come across something in Java that baffles me. I’m not saying there is a better way to do this in any other language. Maybe someone will show the solution in their language of passion.

So, I’ll I want to do is get the difference between one ArrayList and another. And, only one way. Nothing hard about that. So, I thought. The first problem semi-stickler is that I have to get rid of all duplicates first…to make to the diff cleaner. In order to do this (order not a factor) is go from an ArrayList to a HashSet. You can see the rest below. There has to be a better way.

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;

public class HashTest {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<String> aList = new ArrayList<String>();
        ArrayList<String> bList = new ArrayList<String>();

        aList.add("xxx");
        aList.add("yyy");
        aList.add("zzz");
        aList.add("zzz");

        bList.add("xxx");
        bList.add("yyy");
        bList.add("yyy");
        bList.add("aaa");

        HashSet<String> aHash = new HashSet<String>(aList);
        HashSet<String> bHash = new HashSet<String>(bList);

        Iterator<String> aIter = aHash.iterator();
        Iterator<String> bIter = bHash.iterator();

        while (aIter.hasNext()) {
            System.out.println("A: " + aIter.next());
        }

        while (bIter.hasNext()) {
            System.out.println("B: " + bIter.next());
        }

        HashSet<String> diffHash = new HashSet<String>();
        diffHash = aHash;
        diffHash.removeAll(bHash);

        Iterator<String> diffIter = aHash.iterator();
        while (diffIter.hasNext()) {
            System.out.println("A DIFF: " + diffIter.next());
        }
    }
}

Comments
  1. James LorenzenNovember 01, 2007 @ 09:59 PM

    You knew this was coming. First here is the groovy equivalent (which I know is not the perfect solution but it’s a good start):

    def aList = [‘xxx’, ‘yyy’, ‘zzz’, ‘zzz’] def bList = [‘xxx’, ‘yyy’, ‘yyy’, ‘aaa’]

    def aHash = new HashSet(aList) def bHash = new HashSet(bList)

    aHash.each { println “A: $it” }

    bHash.each { println “B: $it” }

    def diffHash = aHash diffHash.removeAll(bHash)

    aHash.eachWithIndex {it, i -> println ”$i A DIFF: $it” }

    However, I think you would agree with me the simplest approach would be to combine groovy’s minus and unique methods:

    def aList = [‘xxx’, ‘yyy’, ‘zzz’, ‘zzz’] def bList = [‘xxx’, ‘yyy’, ‘yyy’, ‘aaa’]

    def diff = aList – bList println diff?.unique()

    Not bad, eh? The question mark ? checks for any NullPointerExceptions (see http://jlorenzen.blogspot.com/2007/10/using-groovy-to-easily-avoid-nasty.html). Too view the dozens of extra methods groovy added to Java see http://groovy.codehaus.org/JN1015-Collections

    You can easily mix groovy and java in your project using the maven2 groovy plugin. http://joe.kueser.com/2007/10/13/more-groovy-goodies/#more-8

  2. meNovember 02, 2007 @ 06:32 AM

    That IS pretty cool. Groovy is now in my list. Curious, what the overhead is…can’t be much.

  3. Joe KueserNovember 02, 2007 @ 07:02 AM

    Stay tuned for Groovy 1.1. There is a lot of performance improvements there. Though even 1.0 doesn’t seem too bad.

  4. James LorenzenNovember 07, 2007 @ 11:43 AM

    Here is a pretty cool website allowing you too compare the performance of different languages in a Nascar kind of way where they are all on equal playing field. Go here to see groovy compared to java: http://shootout.alioth.debian.org/gp4sandbox/benchmark.php?test=all&lang=groovy&lang2=java

    Note they are using groovy 1.0. The current release is 1.0-rc2 where they have made significant performance improvements.

    Here is an FAQ on how you can participate: http://shootout.alioth.debian.org/gp4sandbox/faq.php

Post a comment
Comment