是否可以将比较器传递给 Collections.disjoint? 在文档中我找不到接受比较器的方法的变体,还是我遗漏了什么?
Collections.disjoint(c1, c2);
我可以在 Collections 文档中看到 sort(Collections 方法)有两个版本,一个有比较器,一个没有:
sort(List<T> list)
sort(List<T> list, Comparator<? super T> c)
- 为什么 disjoint 没有接受比较器的版本?
- 如果我不能修改单个对象的类,有什么替代方法可以传递比较器? (在我的例子中,各个对象都是 String 类型,所以我不能覆盖 equals 和 hashcode。)
最佳答案
这个答案介于评论和完整答案之间,但我相信 Collections.disjoint()
没有暴露使用自定义比较器的可能性的原因是它不使用比较器来计算两个集合之间的不相交。
来自Javadoc :
public static boolean disjoint(Collection c1, Collection c2)
Returns true if the two specified collections have no elements in common.
用于确定两个元素是否“共同”的方法是集合中每个条目的 equals()
方法。另一方面,在 Java 中,比较器通常用于确定集合中两个元素的相对顺序。
关于java - 将比较器传递给 java Collections.disjoint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38264252/