Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a comparator to java Collections.disjoint

Is it possible to pass a comparator to Collections.disjoint? In the documentation I couldn't find a variation of the method to be accepting comparator, or am I missing something?

Collections.disjoint(c1, c2);

I can see in Collections documentation that sort (method of Collections) has two versions, one with comparator and one without:

sort(List<T> list)
sort(List<T> list, Comparator<? super T> c)
  • Why disjoint doesn't have a version accepting comparator?
  • What is an alternative way to pass a comparator if I can't modify the class of individual objects? (In my case the individual objects are of type String so I can't override the equals and hashcode.)
like image 366
apadana Avatar asked Jul 08 '16 10:07

apadana


1 Answers

This answer lies somewhere between a comment and a full-blown answer, but I believe the reason that Collections.disjoint() does not expose the possibility of using a custom comparator is that it doesn't use a comparator to compute the disjoint between two collections.

From the Javadoc:

public static boolean disjoint(Collection c1, Collection c2)

Returns true if the two specified collections have no elements in common.

The method which is used to determine if two elements are "in common" is the equals() method of each entry in the collection. On the other hand, in Java a comparator is typically used to determine the relative ordering of two elements in a collection.

like image 179
Tim Biegeleisen Avatar answered Sep 22 '22 16:09

Tim Biegeleisen