简体   繁体   English

在 Java 中使用 lambda 对来自另一个类的对象列表进行排序

[英]Sorting list of objects from another Class with lambda in Java

In my taks I need to sort names with lambda.在我的任务中,我需要使用 lambda 对名称进行排序。 Class lambdaComparator extends class Car. lambdaComparator 类扩展了 Car 类。

I make class LambdaComparator like this:我像这样制作 LambdaComparator 类:

public class LambdaComparator extends Car

public LambdaComparator(String name) {super(name);}

In this class I need to sort objects of type Car.在这个类中,我需要对 Car 类型的对象进行排序。

In main class I have list of objects and with function I need to sort it.在主类中,我有对象列表,并且我需要对其进行排序。

In class LambdaComparator I have this:在 LambdaComparator 类中,我有这个:

Collections.sort(list, new Comparator<Car>() {
         public int compare(Car x, Car y) {
            return x.getName().compareTo(y.getName()));
         }
      });

How should I call function in main to get this sorted, should I make function of type void in class to somehow call it.我应该如何在 main 中调用函数来进行排序,我是否应该在类中创建 void 类型的函数以某种方式调用它。

Edit: lambda expression编辑:lambda 表达式

class LambdaSort<T extends Car>
private List<T> listOfCars;

public LambdaComparator(){
this.listOfCars = new ArrayList<T>();
}

 public void sortCars(T cars)
listOfCars.sort((Car o1, Car o2)->o1.getName().compareTo(o2.getName());

In main function I add objects of type car to that list.在主函数中,我将 car 类型的对象添加到该列表中。

A lambda comparator would be something like this.一个 lambda 比较器就是这样的。

Comparator<Car> comp = (c1, c2)-> c1.getName().compareTo(c2.getName());

In the above example, the Comparator is comparing on a specific field of the Car class, name .在上面的示例中, Comparator 正在比较Car类的特定字段name Since name probably returns a string, one can use compareTo since the String class implements the Comparable (not Comparator ) interface for comparing Strings.由于 name 可能返回一个字符串,因此可以使用compareTo因为 String 类实现了Comparable (而不是Comparator )接口来比较字符串。

But the lambda could be specified much more easily using one of the methods in the Comparator interface.但是可以使用 Comparator 接口中的方法之一更轻松地指定 lambda。 The comparing method may take a lambda or a method reference (shown below). comparing方法可以采用 lambda 或方法引用(如下所示)。

Comparator<Car> comp = Comparator.comparing(Car::getName);

Then when it is passed to the sort method, the sort method will apply it to the objects under sort as comp.compare(obj1, obj2) (although it may not be called comp in the method that uses it)然后当它传递给sort方法时,sort方法会将其作为comp.compare(obj1, obj2)应用于sort下的对象(虽然在使用它的方法中可能不会调用comp

For more information, check out The Java Tutorials有关更多信息,请查看Java 教程

Take a look at what lambda is here .看看这里的lambda 是什么。

If you have a list in your main class, it's as simple as just sorting it.如果你的主类中有一个列表,它就像排序一样简单。 You probably don't even need the class LambdaComparator .您可能甚至不需要LambdaComparator类。

        List<Car> list = new ArrayList<>();
        Collections.sort(list, (a, b) -> a.getName().compareTo(b.getName()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM