简体   繁体   English

Lambda比较器排序列表

[英]Lambda Comparator Sorting List

I have the following task: 我有以下任务:

Implement the following functionality in CompanyTest: 在CompanyTest中实现以下功能:

Create a collection list of all employees from all departments named allEmployees. 创建一个名为allEmployees的所有部门的所有雇员的集合列表。 Sort the list using the sort method (Comparator c), which inherits allEmployees from the List interface. 使用排序方法(比较器c)对列表进行排序,该方法从List接口继承allEmployees。 Implement the comparator with a lambda expression. 用lambda表达式实现比较器。

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()); 
    }
}

I do not know what i am doing wrong. 我不知道我在做什么错。 Can someone help me correct my code. 有人可以帮助我更正我的代码吗? I am still a java noob and all help is appreciated. 我仍然是Java noob,感谢所有帮助。

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;
    }



}

The problem is a signature mismatch between the comparator required by your list and the comparator passed to sort on that list. 问题是列表所需的比较器与传递给该列表的sort比较器之间的签名不匹配。

Your variable declaration: 您的变量声明:

List<String> AllEmployees

Says that the list contains instances of String . 说列表包含String实例。 And the generic sort method in List will take a comparator of the generic type of elements, which means: List的通用sort方法将使用元素的通用类型的比较器,这意味着:

AllEmployees.sort

Accepts a comparator of Strings ( Comparator<String> ). 接受字符串的比较器( Comparator<String> )。

The problem is that your lambda expression for that comparator has the signature of a comparator of Employee : 问题是该比较器的lambda表达式具有Employee的比较器的签名:

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

Can only be used as a Comparator<Employee> . 只能用作Comparator<Employee> And that's why the compiler prevents it from being used in the place of a Comparator<String> , which your AllEmployees list requires. 这就是为什么编译器阻止您的AllEmployees列表要求使用它代替Comparator<String>AllEmployees

You probably meant to add all employee entries to your allEmployees list, then sort that list: 您可能打算将所有员工条目添加到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()));

Note that you need to properly implement the logic of the comparator that dictates the sorting order. 请注意,您需要正确实现指示排序顺序的比较器逻辑。 The above example just compares last names. 上面的示例仅比较姓氏。

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

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