I have these classes:
Object 1
String label
List<Object2>
Object2
String label
List<Object3>
Object3
String label
I want to compare two lists of Object1
.
This is what I did:
public class Object1 implements Comparator<Object1> {
@Override
public int compare(Object1 o1, Object1 o2) {
int compare = o1.getLabel().compareTo(o2.getLabel());
if (compare == 0) {
Iterator<Object2> iterator1 = o1.getObjects2().iterator();
Iterator<Object2> iterator2 = o2.getObjects2().iterator();
while (iterator1.hasNext() && iterator2.hasNext() && compare == 0) {
Object2 t1 = iterator1.next();
Object2 t2 = iterator2.next();
compare = t1.getLabel().compareTo(t2.getLabel());
}
}
return compare;
}
And I did the same for Object2
.
I sort my list of Object1
by calling it in the code by:
SomeObject.getListOfObject1().stream().sorted();
Is there any other method "cleaner" than this? I feel like I am missing something...