Вы можете создать компаратор, чтобы сделать его более читабельным:
public class Test {
int age;
int money;
int id;
public Test(int age, int money, int id) {
this.age = age;
this.money = money;
this.id = id;
}
public static void main(String... args) {
Test t1 = new Test(25,200,3);
Test t2 = new Test(30,50,5);
Test t3 = new Test(15,90,9);
Comparator<Test> comp = Comparator.<Test>comparingInt(x -> x.age)
.thenComparingInt(x -> x.money)
.thenComparingInt(x -> x.id);
Set<Test> set = new TreeSet<>(comp); // Pass the comparator to the Treeset, TreeMap, etc., or use it inside of you Comparable.compareTo method.
set.add(t1);
set.add(t2);
set.add(t3);
System.out.println(set); // [Test{age=15, money=90, id=9}, Test{age=25, money=200, id=3}, Test{age=30, money=50, id=5}]
}
@Override
public String toString() {
return "Test{" + "age=" + age + ", money=" + money + ", id=" + id + '}';
}
}
Как видите, вы можете использовать Comparator.comparingInt (x -> x. headNode )
.thenComparingInt (x -> x. headPeriod2 )
.thenComparingInt (x -> x. tailNode ) ...
и т. Д., Чтобы сделать его более значимым.
Вы можете продолжать добавлять больше этих .thenComparingInt ... по мере роста вашего класса.
Это отсортирует их по headNode, затем по headPeriod2, затем по tailNode и т. Д.
(вместо x используйте любое имя для этой переменной, например (network -> network.headNode)
В Comparator есть больше статических методов и методов экземпляров для создания различных компараторов, которые вы можете зациклить.
Если вы реализуете Comparable и хотите использовать ваш Comparator внутри вашего метода CompareTo , тогда поместите созданный Comparator в качестве поля экземпляра и используйте компаратор внутри compteT, например:
public class Test implements Comparable<Test>{
int age;
int money;
int id;
Comparator<Test> comp = Comparator.<Test>comparingInt(x -> x.age)
.thenComparingInt(x -> x.money)
.thenComparingInt(x -> x.id);
public Test(int age, int money, int id) {
this.age = age;
this.money = money;
this.id = id;
}
public static void main(String... args) {
Test t1 = new Test(25,200,3);
Test t2 = new Test(30,50,5);
Test t3 = new Test(15,90,9);
Set<Test> set = new TreeSet<>();
set.add(t1);
set.add(t2);
set.add(t3);
System.out.println(set); // [Test{age=15, money=90, id=9}, Test{age=25, money=200, id=3}, Test{age=30, money=50, id=5}]
}
@Override
public String toString() {
return "Test{" + "age=" + age + ", money=" + money + ", id=" + id + '}';
}
@Override
public int compareTo(Test o) {
return comp.compare(this, o);
}
}
со ссылкой на метод:
public class Test implements Comparable<Test>{
private int age;
private int money;
private int id;
private final Comparator<Test> comp = Comparator.<Test>comparingInt(Test::getId)
.thenComparingInt(Test::getMoney)
.thenComparingInt(Test::getAge);
public static void main(String... args) {
Test t1 = new Test(25, 200, 3);
Test t2 = new Test(30, 50, 5);
Test t3 = new Test(15, 90, 9);
Set<Test> set = new TreeSet<>();
set.add(t1);
set.add(t2);
set.add(t3);
System.out.println(set); // [Test{age=25, money=200, id=3}, Test{age=30, money=50, id=5}, Test{age=15, money=90, id=9}]
}
public Test(int age, int money, int id) {
this.age = age;
this.money = money;
this.id = id;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public int compareTo(Test o) {
return comp.compare(this, o);
}
@Override
public String toString() {
return "Test{" + "age=" + age + ", money=" + money + ", id=" + id + '}';
}
}
Надеюсь, это поможет.