用例是对自定义对象列表进行排序的有效方式
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 8,您可以执行以下操作:
comments.forEach(oc -> sort(oc.getocRs()))
关于Java 集合和比较器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53518855/