Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Comparing two Lists for equality

splungebobOct 7 2011 — edited Oct 7 2011
I'm trying to determine if 2 Lists contain the same elements, including repeats, but order does not matter. For example:

List 1: A, B, C, D, E
List 2: A, A, B, C, D
List 3: A, B, D, A, C
List 4: A, B, B, C, D

I would consider only Lists 2 and 3 to be equal. I have what I think are 3 working methods, but I'd like your opinion on:

1) Are they actually correct? Do you see an obvious flaw in the algorithm.
2) Efficiency. I always sucked at looking at an algorithm and being able to "see" the big-O of it.
3) Any other ideas.

Also, I'm a little more confident about the correctness of #1 and #2 (efficiency notwithstanding), and less confident about #3. I got the idea for #3 from reading the Javadoc on List's hashCode(). I'm not really sure if it's correct.
import java.util.*;

public class ListCompareTest
{
  public static void main(String[] args)
  {
    List s1 = Arrays.asList(new String[]{"A", "B", "C", "D", "E"});
    List s2 = Arrays.asList(new String[]{"A", "A", "C", "D", "E"});
    List s3 = Arrays.asList(new String[]{"A", "E", "C", "D", "A"});
    
    System.out.println("\nUsing method 1:");
    System.out.println("s1 & s2: " + isEqual(s1, s2));
    System.out.println("s2 & s3: " + isEqual(s2, s3));
    
    System.out.println("\nUsing method 2:");
    System.out.println("s1 & s2: " + isEqual2(s1, s2));
    System.out.println("s2 & s3: " + isEqual2(s2, s3));

    System.out.println("\nUsing method 3:");
    System.out.println("s1 & s2: " + isEqual3(s1, s2));
    System.out.println("s2 & s3: " + isEqual3(s2, s3));
  }
  
  public static boolean isEqual(List a, List b)
  {
    if (a.size() != b.size())
    {
      return false;
    }
    
    List copyB = new ArrayList(b);
    
    for (Object obj : a)
    {
      if (! copyB.remove(obj))
      {
        return false;
      }
    }
    
    return true;
  }
  
  public static boolean isEqual2(List a, List b)
  {
    if (a.size() != b.size())
    {
      return false;
    }
    
    List copyA = new ArrayList(a);
    List copyB = new ArrayList(b);
    
    Collections.sort(copyA);
    Collections.sort(copyB);
    
    for (int i = 0; i < copyA.size(); i++)
    {
      if (! copyA.get(i).equals(copyB.get(i)))
      {
        return false;
      }
    }
    
    return true;
  }
  
  public static boolean isEqual3(List a, List b)
  {
    if (a.size() != b.size())
    {
      return false;
    }
    
    List copyA = new ArrayList(a);
    List copyB = new ArrayList(b);
    
    Collections.sort(copyA);
    Collections.sort(copyB);
    
    return copyA.hashCode() == copyB.hashCode();
  }
}
This post has been answered by 796440 on Oct 7 2011
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 4 2011
Added on Oct 7 2011
18 comments
297 views