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 & then converting to a Set). Maybe there exists a library in Guava which does this?
public class CompareUnorderedList implements BiPredicate<Object, Object> {
private Function<Object, Object> mapping;
public CompareUnorderedList(Function<Object, Object> 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<Object> firstSet = (List<Object>o1).stream().map(mapping).collect(Collectors.toSet());
Set<Object> secondSet = (List<Object>o2).stream().map(mapping).collect(Collectors.toSet());
return firstSet.equals(secondSet);
}
}
Objectinstead ofCollection<?>or evenList<?>... why is that the case?? \$\endgroup\$