【发布时间】:2021-03-02 13:18:49
【问题描述】:
我有一组具有name 属性的对象。其中一些对象应该按升序显示在集合的开头,其余的也应该在开头的元素之后按升序排序。集合中的 cmets 是集合的内容。
这里有一些例子:
预期输出为("abc", "hello", "a", "c", "f", "test")
Set<Props> props = handler.getProps(); //("test", "abc", "c", "f", "hello", "a")
Set<Props> unnecessaryProps = handler.getUnnecessaryProps(); //("hello", "abc")
Comparator comparator = new Comparator<Props>() {
@Override
public int compare(Props e1, Props e2) {
if (e1.equals(e2)) {
return 0;
}
if (unnecessaryProps.contains(e1)) {
return -1;
}
if (unnecessaryProps.contains(e2)) {
return 1;
}
return e1.compare(e2);
}
};
}
谁能帮帮我?
【问题讨论】:
-
这个比较器看起来不对:如果 e1 和 e2 都在不必要的属性中,它应该返回 0(或 e1.compare(e2))。
标签: java sorting set comparator