简体   繁体   中英

Lambda Comparator Sorting List

I have the following task:

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

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.

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.

Your variable declaration:

List<String> AllEmployees

Says that the list contains instances of String . And the generic sort method in List will take a comparator of the generic type of elements, which means:

AllEmployees.sort

Accepts a comparator of Strings ( Comparator<String> ).

The problem is that your lambda expression for that comparator has the signature of a comparator of Employee :

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

Can only be used as a 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.

You probably meant to add all employee entries to your allEmployees list, then sort that list:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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