gpt4 book ai didi

java - 是否有一种有效且更快的方法来重写 TreeSet 中使用的compareTo()方法

转载 作者:行者123 更新时间:2023-12-01 16:44:10 24 4
gpt4 key购买 nike

我读取了一个包含六列的表,并将其传递到 TreeSet 集合中。它确实工作正常,但是,我只是好奇是否有更有效的方法来覆盖 compareTo() 方法。提出这个问题的原因是我将有更多的组和更多的列,而我这样做的方式在我看来效率低下且耗时。需要注意的是,我的类中的所有元素都是整数。

另外,我还有一个问题。 compareTo() 方法的工作之一是否包含像 HashMap() 中的 HashCode() 那样防止重复?

下面我将展示如何定义 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.headNode) .thenComparingInt(x -> x.headPeriod2) .thenComparingInt(x -> x.tailNode)...

等,使其更有意义。随着类(class)的增长,您可以继续添加更多 .thenComparingInt... 。这将按 headNode 对它们进行排序,然后按 headPeriod2,然后按 tailNode,依此类推。

(代替 x,使用您想要的变量名称,例如 (network -> network.headNode)

Comparator 中有更多静态方法和实例方法来创建可以循环的不同比较器。

如果您实现 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 + '}';
}
}

希望有帮助。

关于java - 是否有一种有效且更快的方法来重写 TreeSet 中使用的compareTo()方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56137988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com