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

book
Challenge: Sorting Employees

Uppgift

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ösning

java

solution

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;
Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 1. Kapitel 10
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;

Fråga AI

expand
ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

some-alt