My solution is for the cases where you don't care about the ordering within the Lists
- in other words: Lists
with the same elements but DIFFERENT ordering will be considered to have the same contents.
Example: ["word1", "word2"]
and ["word2", "word1"]
is considered to have the same content.
I've addressed ordering, I also just need to say something about duplicates. Lists
need to have the same number of elements to be considered equal.
Example: ["word1"]
and ["word1", "word1"]
is considered to NOT have the same content.
My solution:
public class ListUtil {
public static <T> boolean hasSameContents(List<T> firstList, List<T> secondList) {
if (firstList == secondList) { // same object
return true;
}
if (firstList != null && secondList != null) {
if (firstList.isEmpty() && secondList.isEmpty()) {
return true;
}
if (firstList.size() != secondList.size()) {
return false;
}
List<T> tmpSecondList = new ArrayList<>(secondList);
Object currFirstObject = null;
for (int i=1 ; i<=firstList.size() ; i++) {
currFirstObject = firstList.get(i-1);
boolean removed = tmpSecondList.remove(currFirstObject);
if (!removed) {
return false;
}
if (i != firstList.size()) { // Not the last element
if (tmpSecondList.isEmpty()) {
return false;
}
}
}
if (tmpSecondList.isEmpty()) {
return true;
}
}
return false;
}
}
I've tested it with Strings
as follows:
@Test
public void testHasSameContents() throws Exception {
// comparing with same list => no duplicate elements
Assert.isTrue(ListUtil.hasSameContents(List.of("one", "two", "three"), List.of("one", "two", "three")));
// comparing with same list => duplicate elements
Assert.isTrue(ListUtil.hasSameContents(List.of("one", "two", "three", "one"), List.of("one", "two", "three", "one")));
// compare with disordered list => no duplicate elements
Assert.isTrue(ListUtil.hasSameContents(List.of("one", "two", "three"), List.of("three", "two", "one")));
// compare with disordered list => duplicate elements
Assert.isTrue(ListUtil.hasSameContents(List.of("one", "two", "three", "one"), List.of("three", "two", "one", "one")));
// comparing with different list => same size, no duplicate elements
Assert.isFalse(ListUtil.hasSameContents(List.of("one", "two", "three"), List.of("four", "five", "six")));
// comparing with different list => same size, duplicate elements
Assert.isFalse(ListUtil.hasSameContents(List.of("one", "two", "two"), List.of("one", "two", "three")));
Assert.isFalse(ListUtil.hasSameContents(List.of("one", "two", "three"), List.of("one", "two", "two")));
// comparing with different list => different size, no duplicate elements
Assert.isFalse(ListUtil.hasSameContents(List.of("one", "two", "three", "four"), List.of("one", "two", "three")));
Assert.isFalse(ListUtil.hasSameContents(List.of("one", "two", "three"), List.of("one", "two", "three", "four")));
// comparing with different list => different sizes, duplicate elements
Assert.isFalse(ListUtil.hasSameContents(List.of("one", "two", "three", "one"), List.of("one", "two", "three")));
Assert.isFalse(ListUtil.hasSameContents(List.of("one", "two", "three"), List.of("one", "two", "three", "one")));
}