猿问

我怎么能在一个类中编写多个“compareTo”方法?

我正在按两个对象比较和排序多个属性,但为此我创建了不同的类来比较这些不同的属性。


这是在我的主要课程中:


Collections.sort(Comparator, new SortByRef());

        System.out.println("\nOrdenado por Referencia");

        for(int i=0; i<Comparator.size(); i++)

            System.out.println(Comparator.get(i));



        Collections.sort(Comparator, new SortByMonto());

        System.out.println("\nOrdenado por Monto");

        for(int i=0; i<Comparator.size(); i++)

            System.out.println(Comparator.get(i));


        Collections.sort(Comparator, new SortByDes());

        System.out.println("\nOrdenado por Descripcion");

        for(int i=0; i<Comparator.size(); i++)

            System.out.println(Comparator.get(i));


        Collections.sort(Comparator, new SortByNP());

        System.out.println("\nOrdenado por Numero de parte");

        for(int i=0; i<Comparator.size(); i++)

            System.out.println(Comparator.get(i));

如您所见,在 中Collections.sort,我SortBy...为每个类创建了不同的,我的意思是,不同的类。


但我只想创建一个类来比较所有属性。我怎么能做到?


例如,这些是我创建的方法:


public int compare(COSTOS ref1, COSTOS ref2) {

        return ref1.referencia - ref2.referencia;}


public int compare(COSTOS monto1, COSTOS monto2) {

        return (int) (monto1.monto - monto2.monto);

    }


慕的地6264312
浏览 260回答 2
2回答

弑天下

我认为,您不想为每个排序顺序创建一个单独的类。但它不像你尝试的那样工作。我认为, Comparator 是一个类型为 List 的变量。也许你应该尝试这样的事情:comparator.sort(comparing(c -> c.referencia));System.out.println("\nOrdenado por Referencia");comparator.forEach(System.out::println);这里的“比较”方法是从 Java 类库的类型 Comparator 静态导入的。这使您无需新命名的顶级类即可成为新的比较器。您应该以小写字母开头变量名(此处:Comparator)。这是 Java 中的通用约定。

BIG阳

Comparator您可以创建多个进行比较的方法,而不是创建多个实现接口的类。然后,方法本身基本上是(!) 的实现Comparator,您可以Collections#sort使用方法引用将其传递给方法。根据比较实际发生的方式,更简洁的形式是使用这些Comparator#comparing方法,正如Donat 在他的回答 (+1) 中提到的那样。具体来说,您可以comparingInt在示例中使用。此处显示了两种方法:import java.util.ArrayList;import java.util.Collections;import java.util.Comparator;import java.util.List;public class MultipleCompareMethods{&nbsp; &nbsp; public static void main(String[] args)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; List<Costos> list = new ArrayList<Costos>();&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Costos(1,3));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Costos(4,5));&nbsp; &nbsp; &nbsp; &nbsp; list.add(new Costos(2,7));&nbsp; &nbsp; &nbsp; &nbsp; // Using Method references&nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(list, MultipleCompareMethods::compareByReferencia);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("\nOrdenado por Referencia");&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < list.size(); i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(list.get(i));&nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(list, MultipleCompareMethods::compareByMonto);&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("\nOrdenado por Monto");&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < list.size(); i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(list.get(i));&nbsp; &nbsp; &nbsp; &nbsp; // Using Comparator#comparingInt:&nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(list, Comparator.comparingInt(c -> c.referencia));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("\nOrdenado por Referencia");&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < list.size(); i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(list.get(i));&nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(list, Comparator.comparingInt(c -> c.monto));&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("\nOrdenado por Monto");&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < list.size(); i++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(list.get(i));&nbsp; &nbsp; }&nbsp; &nbsp; static int compareByReferencia(Costos c0, Costos c1)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return Integer.compare(c0.referencia, c1.referencia);&nbsp; &nbsp; }&nbsp; &nbsp; static int compareByMonto(Costos c0, Costos c1)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return Integer.compare(c0.monto, c1.monto);&nbsp; &nbsp; }}class Costos{&nbsp; &nbsp; int referencia;&nbsp; &nbsp; int monto;&nbsp; &nbsp; Costos(int referencia, int monto)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.referencia = referencia;&nbsp; &nbsp; &nbsp; &nbsp; this.monto = monto;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return "COSTOS [referencia=" + referencia + ", monto=" + monto + "]";&nbsp; &nbsp; }}class Costos{&nbsp; &nbsp; int referencia;&nbsp; &nbsp; int monto;&nbsp; &nbsp; Costos(int referencia, int monto)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; this.referencia = referencia;&nbsp; &nbsp; &nbsp; &nbsp; this.monto = monto;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; return "COSTOS [referencia=" + referencia + ", monto=" + monto + "]";&nbsp; &nbsp; }}如果你getReferencia在你的类中提供了方法,这可以更简洁地完成,再次使用方法引用:Collections.sort(list, Comparator.comparingInt(Costos::getReferencia));(从 Java 8 开始,几乎不需要实际实现自己的比较器类......)
随时随地看视频慕课网APP

相关分类

Java
我要回答