Find the Difference Between Two Lists in Java

Let’s say you have two lists in Java that have a lot of overlap between them.

ArrayList a = new ArrayList();
a.add("abc");
a.add("def");
a.add("ghi");
 
ArrayList b = new ArrayList();
b.add("def");
b.add("ghi");
b.add("jkl");

Now let’s say you want to determine what is in b that is not in a. In set theory, you would refer to this as the set-theoretic difference of b in a. The following method would accomplish this.

public static Collection Subtract(Collection coll1, Collection coll2)
{
    Collection result = new ArrayList(coll2);
    result.removeAll(coll1);
    return result;
}

Now let’s say you wanted to know which elements in the two lists did not overlap at all between them. This is sometimes referred to as the symmetric difference. The following function should serve this purpose. (Note that this uses the Union and Intersection approaches as described here and here.)

public static Collection GetNonOverlapping(Collection coll1, Collection coll2)
{
    Collection result = Union(coll1, coll2);
    result.removeAll(Intersect(coll1, coll2));
    return result;
}

You could test these in this way. (You would expect the first to give you only “jkl” and the second to give you “abc” and “jkl”).

System.out.println("Subtraction:");
for (Object x : Lists.Subtract(a, b))
    System.out.println(x);
 
System.out.println("Non Overlap:");	    
for (Object x : Lists.GetNonOverlapping(a, b))
    System.out.println(x);

8 Responses to “Find the Difference Between Two Lists in Java”

  1. Hi,

    My arraylist of type X, now when I use above code it adds all the values into list. Is that because I have not overridden equals()..?

    Can you help me with this, I’m not getting how shall I overide equals() for this. Objects which I have to compare are of type cell.

  2. Yes, most likely you will need to override equals. Here’s a post on this in case you haven’t already seen this. If this doesn’t work, then please post a code sample, and I’ll see what I can figure out.

    http://code.hammerpig.com/how-to-compare-two-objects-in-java-by-overriding-equals.html

  3. Hello,

    Thank you for this example, but the call to remove all produces an arrayList full of nulls. I have over-ridden equals and hash code for my objects, any insight would be greatly appreciated.

  4. Hi Alex,

    Can you give me a simple code sample of what you’re trying to do?

  5. public static Collection subtract(Collection col1, Collection col2)
    {
    Collection result = new ArrayList(col2);
    result.removeAll(col1);
    return result;
    }//when result.removeAll returns the list is full of nulls
    //Also the I have defined equals in the entity class.

  6. It seems to be working for me. Here’s a simple code example. Try this out, and let me know what’s different about your code that might be making a difference.

    public class Entity
    {
    public String Property1;
    public int Property2;

    public Entity(String property1, int property2)
    {
    Property1 = property1;
    Property2 = property2;
    }

    @Override
    public boolean equals(Object obj)
    {
    if (this == obj)
    return true;
    if (obj == null)
    return false;
    if (!(obj instanceof Entity))
    return false;

    Entity compareEntity = (Entity)obj;

    return this.Property1.equals(compareEntity.Property1) && this.Property2 == compareEntity.Property2;
    }
    }

    public static Collection subtract(Collection col1, Collection col2)
    {
    Collection result = new ArrayList(col2);
    result.removeAll(col1);
    return result;
    }

    public void Test()
    {
    ArrayList list1 = new ArrayList();
    ArrayList list2 = new ArrayList();

    list1.add(new Entity(“abc”, 1));
    list1.add(new Entity(“abc”, 2));
    list1.add(new Entity(“abc”, 3));
    list2.add(new Entity(“abc”, 1));
    list2.add(new Entity(“abc”, 4));

    ArrayList list3 = new ArrayList(subtract(list2, list1));

    for (Entity entity : list3)
    System.out.println(entity.Property1 + ” – ” + String.valueOf(entity.Property2));
    }

  7. ah, works now, thanks for the example!

  8. Ivan S Kirkpatrick on May 31st, 2011 at 1:22 pm

    I found this very helpful. However, in my testing which uses this, I need to know what were the differences. I will have to add some more code to get the output I need. Also I recall using the term disjoint set in my Math classes.

Leave a Reply