Fork me on GitHub
Subscribe to RSS Feed

Kit Plummer
Software Engineer :: Techitect :: Evangelist
kitplummer@gmail.com
kitplummer (AIM,Yahoo!IM,Gtalk,Skype)

To the top »

Please share:

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 aList = new ArrayList();
ArrayList bList = new ArrayList();

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 aHash = new HashSet(aList);
HashSet bHash = new HashSet(bList);

Iterator aIter = aHash.iterator();
Iterator bIter = bHash.iterator();

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

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

HashSet diffHash = new HashSet();
diffHash = aHash;
diffHash.removeAll(bHash);

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


Please comment:
blog comments powered by Disqus