Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Challenge: Sorting Employees | Fundamentals and Functional Capabilities of Stream API
Stream API

book
Challenge: Sorting Employees

Aufgabe

Swipe to start coding

Implement a Comparator to sort the list of employees based on the specified criteria.

  1. Sort the employees by years of experience in ascending order, and then apply the reversed() method to make the order descending.
  2. If the years of experience are the same, sort the employees with the same experience by salary in descending order.
  3. If both experience and salary are the same, sort by name in alphabetical order.
  4. Use the created comparator as an argument for the sort() method to sort the employee list.
  5. Print the sorted list of employees.

Lösung

solution.java

solution.java

package com.example;

import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Comparator;

public class Main {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>(Arrays.asList(
new Employee("Michael", 5, 90000, "HR"),
new Employee("Emily", 3, 65000, "IT"),
new Employee("James", 5, 95000, "Marketing"),
new Employee("Sarah", 5, 90000, "IT"),
new Employee("David", 2, 50000, "Finance")
));

// Comparator: Sort by years of experience (descending), then salary (descending), then name (alphabetical)
Comparator<Employee> employeeComparator = Comparator
.comparing(Employee::getYearsOfExperience).reversed()
.thenComparing(Employee::getSalary, Comparator.reverseOrder())
.thenComparing(Employee::getName);

employees.sort(employeeComparator);
employees.forEach(System.out::println);
}
}

class Employee {
private final String name;
private final int yearsOfExperience;
private final double salary;
private final String department;

public Employee(String name, int yearsOfExperience, double salary, String department) {
this.name = name;
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 10
single

single

package com.example;

import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Comparator;

public class Main {
public static void main(String[] args) {
List<Employee> employees = new ArrayList<>(Arrays.asList(
new Employee("Michael", 5, 90000, "HR"),
new Employee("Emily", 3, 65000, "IT"),
new Employee("James", 5, 95000, "Marketing"),
new Employee("Sarah", 5, 90000, "IT"),
new Employee("David", 2, 50000, "Finance")
));

// Comparator: Sort by years of experience (descending), then salary (descending), then name (alphabetical)
Comparator<Employee> employeeComparator = Comparator
.___(___).___()
.___(___, Comparator.reverseOrder())
.___(___);

employees.sort(___);
employees.forEach(System.out::println);
}
}

class Employee {
private final String name;
private final int yearsOfExperience;
private final double salary;
private final String department;

public Employee(String name, int yearsOfExperience, double salary, String department) {
this.name = name;

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

We use cookies to make your experience better!
some-alt