【发布时间】:2018-11-28 11:52:34
【问题描述】:
用例是一种对自定义对象列表进行排序的有效方法
class oC extends A{
int id;
ArrayList ocR;
}
class ocR extends A{
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public boolean equals(Object o) {
return super.equals(o);
}
}
oC的每个对象的ArrayList ocR也必须是有序的。
当前的解决方案是我创建了一个实现比较器的排序函数
sort(List items){
Collections.sort(items, new Comparator(){ //Logic for comparator by
overriding compare(A a, A b)}
}
然后我使用下面的代码
List Oc comments;
for(int index=0;index<comments.size();index++){
//This is for sorting ArrayList ocR of each oC object
this.sort(comments.get(index).getocRs());
}
//This is for sorting List of oC
this.sort(comments);`
有没有办法在不在 oC 或 oCR 类上实现比较器的情况下取消 for 循环?
【问题讨论】:
标签: java collections comparator