Challenge: Finding the Top 3 Hardest-Working Employees
Tehtävä
Swipe to start coding
Several employees work at the factory, and we need to identify the three hardest-working employees based on the number of hours worked.
- Sort the list of
workers
in ascending order by the number ofhoursWorked
using thecomparingInt()
method. - Reverse the list of
workers
to make it descending by the number ofhoursWorked
using thereversed()
method. - Select the top three workers with the highest
hoursWorked
using thelimit()
method. - Print each of these three workers to the console.
Ratkaisu
solution.java
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.example;
import java.util.List;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
List<Worker> workers = List.of(
new Worker("John", 160),
new Worker("Michael", 200),
new Worker("Andrew", 180),
new Worker("David", 220),
new Worker("Robert", 175)
);
List<Worker> topWorkers = workers.stream()
.sorted(Comparator.comparingInt(Worker::getHoursWorked).reversed())
.limit(3)
.toList();
topWorkers.forEach(System.out::println);
}
}
class Worker {
private String name;
private int hoursWorked;
public Worker(String name, int hoursWorked) {
this.name = name;
this.hoursWorked = hoursWorked;
}
public String getName() {
return name;
}
Oliko kaikki selvää?
Kiitos palautteestasi!
Osio 2. Luku 10
single
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package com.example;
import java.util.List;
import java.util.Comparator;
public class Main {
public static void main(String[] args) {
List<Worker> workers = List.of(
new Worker("John", 160),
new Worker("Michael", 200),
new Worker("Andrew", 180),
new Worker("David", 220),
new Worker("Robert", 175)
);
List<Worker> topWorkers = workers.stream()
.sorted(Comparator.comparingInt(___).___)
.___
.toList();
topWorkers.forEach(___);
}
}
class Worker {
private String name;
private int hoursWorked;
public Worker(String name, int hoursWorked) {
this.name = name;
this.hoursWorked = hoursWorked;
}
public String getName() {
return name;
}
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme