[英]Is there an efficient and quicker way to override compareTo() method used in TreeSet
我讀了一個包含六列的表,並傳遞給TreeSet
集合。 它正常工作,但是,我只是好奇,如果有過乘坐更有效的方式compareTo()
方法。 提出這個問題的原因是,我將擁有帶有更多列的額外集合,而我這樣做的方式看來效率如此低下且耗時。 重要的是要注意,我所有的class元素都是整數。
另外,我還有一個問題。 與HashCode()
在HashMap()
所做的一樣, compareTo()
方法的工作之一是否包含防止重復的內容?
下面我展示了如何定義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;
}
}
}
}
}
您可以創建一個Comparator使其更具可讀性:
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)
比較器中還有更多的靜態方法和實例方法可創建可循環使用的不同比較器。
如果您實現Comparable並想在compareTo方法中使用Comparator ,則將創建的Comparator設置為實例字段,並在comparteTo內使用比較器,如下所示:
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;
}
}
}
}
}
無論您這樣做如何,我都極力避免將值相減以獲得比較器的<或>結果。 它可能導致錯誤,並且是一個壞習慣。 查看以下內容:
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;
}
只需使用Comparable功能接口來整理您的要求。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.