Comparing unordered list in java I want to compare two unordered list and currently I am doing it in the following way. Is there a better way to do this as I am repeating my code twice (for null check &amp; then converting to a Set). Maybe there exists a library in Guava which does this? 

public class CompareUnorderedList implements BiPredicate&lt;Object, Object&gt; {
    private Function&lt;Object, Object&gt; mapping;
    public CompareUnorderedList(Function&lt;Object, Object&gt; mapping) {
       this.mapping = mapping;
    }
    @Override
    public boolean test(Object o1, Object o2) {
        o1 = o1 != null ? o1 : Collections.EMPTY_LIST;
        o2 = o2 != null ? o2 : Collections.EMPTY_LIST;

        Set&lt;Object&gt; firstSet = (List&lt;Object&gt;o1).stream().map(mapping).collect(Collectors.toSet());
        Set&lt;Object&gt; secondSet = (List&lt;Object&gt;o2).stream().map(mapping).collect(Collectors.toSet());
        return firstSet.equals(secondSet);
    }
}
I think it would be better to not convert the List to a Set, but just check if they are the same size and if one contains all of the other (as it is done in HashSet) like so:

public static boolean test(Object o1, Object o2){
    try{
        Collection&lt;?&gt; c1 = (Collection&lt;?&gt;)o1,
                      c2 = (Collection&lt;?&gt;)o2;
        return (c1.size()==c2.size())&amp;&amp;c1.containsAll(c2);
    }
    catch(Exception e){
        return false;
    }
}