java - Lambda 比较器排序列表

标签 java lambda comparator

我有以下任务:

Implement the following functionality in CompanyTest:

Create a collection list of all employees from all departments named allEmployees. Sort the list using the sort method (Comparator c), which inherits allEmployees from the List interface. Implement the comparator with a lambda expression.

public class CompanyTest1 {
    public static void main(String[]args) {

        Sequence<Integer> MNR = new Sequence<Integer>(){
            int id = 0;
            public Integer next() {
                return id++;
            }
        };

        List<String> AllEmployees = new ArrayList<>();


        Department dep1 = new Department("Verwaltung", MNR);
        Department dep2 = new Department("Lager", MNR);
        dep1.addEmployee("Chris", "Schmidt"); 
        dep1.addEmployee("Max", "Mustermann");
        dep2.addEmployee("Müller", "Wagner");
        System.out.println("Department: " +dep1.getName());
        dep1.forEach(Employee -> System.out.println(Employee));
        System.out.println("Department: " +dep2.getName());
        dep2.forEach(Employee -> System.out.println(Employee));
        AllEmployees.sort((Comparator<? super E> c) (Employee e1, Employee e2)->e1.getLastName()-e2.getFirstname()); 
    }
}

我不知道我做错了什么。有人可以帮我纠正我的代码吗?我仍然是一个 java 菜鸟,感谢所有帮助。

import java.util.Iterator;

import java.util.*;

public class Department implements Iterable<Employee> {
    private String name;
    private Map<Integer, Employee> employees;
    private Sequence<Integer> employeeIdSequence;


    public Department(String name, Sequence<Integer> employeeIdSequence){
        this.name = name;
        employees = new HashMap<Integer, Employee>();
        this.employeeIdSequence = employeeIdSequence;
    }


    public String getName() {
        return name;
    }

    public Sequence<Integer>  addEmployee(String firstName, String lastName) {
        int neueMitarbeiterNummer = employeeIdSequence.next();
        employees.put(neueMitarbeiterNummer, new Employee(firstName, lastName));
        return employeeIdSequence;


//      Employee a = new Employee(firstName, lastName);
//      int id = employeeIdSequence.next();
//      employees.put(id, a);

    }


    @Override
    public Iterator<Employee> iterator() {
        return employees.values().iterator();
    }

}
public class Employee {
    private String firstName;
    private String lastName;

    public Employee(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public String getFirstname() {
        return firstName;
    }
    public String getLastName() {
        return lastName;
    }

    public String toString() {
        return "Vorname: " + firstName + " " + "Nachname: " + lastName;
    }



}

最佳答案

问题是列表所需的比较器与传递给 sort 的比较器之间的签名不匹配。在那个名单上。

你的变量声明:

List<String> AllEmployees

表示列表包含 String 的实例。和通用sort List中的方法将采用元素的通用类型的比较器,这意味着:

AllEmployees.sort

接受字符串比较器 ( Comparator<String> )。

问题是该比较器的 lambda 表达式具有比较器 Employee 的签名:

(Employee e1, Employee e2)->e1.getLastName()-e2.getFirstname()

只能用作Comparator<Employee> 。这就是为什么编译器阻止它被用来代替 Comparator<String> 的原因。 ,您的AllEmployees列表需要。

您可能打算将所有员工条目添加到您的 allEmployees 中列表,然后对该列表进行排序:

List<Employee> allEmployees = new ArrayList<>();
dep1.forEach(employee -> allEmployees.add(employee));
dep2.forEach(employee -> allEmployees.add(employee));

//And run sort
allEmployees.sort(
(Employee e1, Employee e2) ->  e1.getLastName().compareTo(e2.getLastName()));

请注意,您需要正确实现规定排序顺序的比较器逻辑。上面的例子只是比较姓氏。

关于java - Lambda 比较器排序列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50622776/

相关文章:

c++ - Lambda 捕获 QFile 对象

lambda - java8中的lambda有默认变量占位符吗?

c# - 是否有一种内置的方法来比较 IEnumerable<T> (通过它们的元素)?

带有尾随零的 Java BitSet

java - 在没有任何中间文件的情况下在 Java 中创建 GraphViz 图

c++ - lambda 中引用捕获的对象的类型

java - 如何根据一些规则按降序创建优点列表?

java - 连接到 postgreSQL 服务器失败

java - 仅在某些输入上附加内容的正则表达式

java - 使用 Comparator.nullsLast 时出现 NullPointerException