Problem:
Given two Collection<?>s, check to see if they contain the same elements.
- Assuming the actual implementation of the collection is unknown
- Assuming the elements do not occur in the same order
- Assuming no items are found twice in the same collection
Solution 1:
boolean equals = c1.containsAll(c2) && c2.containsAll(c1);
Solution 2:
boolean equals = new HashSet<?>(c1).equals(new HashSet<?>(c2));
I would suggest that Solution 2 is more efficient (O (n)) than Solution 1 (O (n ^ 2)).
Will I fix it or miss something?
source
share