我读了一个包含六列的表,然后传递到TreeSet
集合中。它确实可以正常工作,但是,我很好奇是否有更有效的骑行compareTo()
方法的方法。要求这样做的原因是,我将拥有更多的列表,其中包含更多的列,而我这样做的方式似乎是如此效率和耗时。重要的是要注意,所有我的班级元素都是整数。
另外,我还有一个额外的问题。compareTo()
方法的工作之一是否包含像HashCode()
中的CC_4一样防止重复的?
下面我显示了如何定义compareTo()
方法。
public int compareTo(Network o) {
int r = this.headNode > o.headNode? 1 : this.headNode < o.headNode ? -1 : 0;
if(r==0) { r = this.headPeriod1 > o.headPeriod1? 1 : this.headPeriod1 < o.headPeriod1? -1 : 0;
if(r==0) {
r = this.headPeriod2 > o.headPeriod2? 1 : this.headPeriod2 < o.headPeriod2? -1 : 0;
if(r==0) {
r = this.tailNode > o.tailNode? 1 : this.tailNode < o. tailNode? -1 : 0;
if(r==0) {
r = this.tailPeriod1 > o.tailPeriod1 ? 1 : this.tailPeriod1 < o.tailPeriod1 ? -1 : 0;
if(r==0) {
r = this.tailPeriod2 > o.tailPeriod2 ? 1 : this.tailPeriod2 < o.tailPeriod2 ? -1 : 0;
}
}
}
}
}
您可以创建一个比较器以更可读性:
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。 .thencomparingint(x-> x。 .thencomparingint(x-> x。
等,使其更有意义。您可以继续添加更多这些。thencomparingint ...随着班级的增长。这将用头节,然后用headperiod2,然后通过尾部节点对它们进行排序。
。(而不是x,请使用您想要的任何名称,例如(网络 -> network.headnode(
比较器中还有更多的静态和实例方法来创建可以循环的不同比较器。
如果您实现了可比性并想在比较方法内使用比较器,则将比较器作为实例字段创建并使用Comparte Onceparto中的比较器,例如:
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 + '}';
}
}
希望它有帮助。
这将有点短/更简单:
public int compareTo(Network o) {
int r = this.headNode - o.headNode;
if (r == 0) {
r = this.headPeriod1 - o.headPeriod1;
if (r == 0) {
r = this.headPeriod2 - o.headPeriod2;
if (r == 0) {
r = this.tailNode - o.tailNode;
if (r == 0) {
r = this.tailPeriod1 - o.tailPeriod1;
if (r == 0) {
r = this.tailPeriod2 - o.tailPeriod2;
}
}
}
}
}
但是,我强烈避免减去值以获取&lt;或>比较器的结果。它可能导致错误,并且是一个坏习惯。查看以下内容:
int val1 = -1223222022;
int val2 = 2130200022;
int result = compareTo(val1, val2);
// This shows val1 > val2
if (result < 0) {
System.out.println(val1 + " < " + val2);
}
else if (result > 0) {
System.out.println(val1 + " > " + val2);
}
val1 = 1234450392;
val2 = -2022030049;
result = compareTo(val1, val2);
//this shows val2 < val2
if (result < 0) {
System.out.println(val1 + " < " + val2);
}
else if (result > 0) {
System.out.println(val1 + " > " + val2);
}
}
public static int compareTo(int a, int b) {
return a - b;
}
只需使用与您的要求相当的功能接口。