|
1 | | -# Data Set |
2 | | - |
3 | | -```java |
4 | | - List<Footballer> getFootballers() { |
5 | | - return List.of( |
6 | | - new Footballer("Messi", 32, Gender.MALE, List.of("CF", "CAM", "RF")), |
7 | | - new Footballer("Ibrahim", 28, Gender.MALE, List.of("CF", "CAM", "LF")), |
8 | | - new Footballer("Arthur", 23, Gender.MALE, List.of("CM", "CAM")), |
9 | | - new Footballer("Cristiano Ronaldo", 27, Gender.MALE, List.of("GK")), |
10 | | - new Footballer("Surinder", 20, Gender.MALE, List.of("CM", "CDM")), |
11 | | - new Footballer("Jennifer", 29, Gender.FEMALE, List.of("CF", "CAM")), |
12 | | - new Footballer("Jana", 17, Gender.FEMALE, List.of("CB")), |
13 | | - new Footballer("Alexia", 25, Gender.FEMALE, List.of("CAM", "RF", "LF")) |
14 | | - ); |
15 | | -} |
16 | | -``` |
17 | | - |
18 | 1 | # Sorted: |
19 | 2 |
|
20 | 3 | The `sorted` method is used to sort the stream. |
21 | 4 |
|
22 | | -#### Example: Sort footballers by Gender and if they have the same gender sort by their names. |
23 | 5 | ```java |
24 | | - List<Footballer> sortByGenderAndName= footballerList.stream() |
25 | | - .sorted(Comparator.comparing(Footballer::getGender).thenComparing(Footballer::getName)) |
26 | | - .collect(Collectors.toList()); |
| 6 | +List<Integer> result = List.of(5, 2, 4, 1, 3).stream() |
| 7 | + .sorted() |
| 8 | + .toList(); |
27 | 9 |
|
| 10 | +System.out.println(result); |
28 | 11 | ``` |
29 | 12 |
|
30 | 13 | #### Output: |
31 | 14 |
|
32 | 15 | ```java |
33 | | -sortByGenderAndName =[ |
34 | | - |
35 | | -Footballer { |
36 | | - name = 'Arthur', age = 23, gender = MALE, positions =[CM, CAM]}, |
37 | | - |
38 | | -Footballer { |
39 | | - name = 'Ibrahim', age = 28, gender = MALE, positions =[CF, CAM, LF]}, |
40 | | - |
41 | | -Footballer { |
42 | | - name = 'Messi', age = 32, gender = MALE, positions =[CF, CAM, RF]}, |
43 | | - |
44 | | -Footballer { |
45 | | - name = 'Surinder', age = 20, gender = MALE, positions =[CM, CDM]}, |
46 | | - |
47 | | -Footballer { |
48 | | - name = 'Cristiano Ronaldo', age = 27, gender = MALE, positions =[GK]}, |
49 | | - |
50 | | -Footballer { |
51 | | - name = 'Alexia', age = 25, gender = FEMALE, positions =[CAM, RF, LF]}, |
52 | | - |
53 | | -Footballer { |
54 | | - name = 'Jana', age = 17, gender = FEMALE, positions =[CB]}, |
55 | | - |
56 | | -Footballer { |
57 | | - name = 'Jennifer', age = 29, gender = FEMALE, positions =[CF, CAM]}, |
58 | | - ] |
| 16 | +[1, 2, 3, 4, 5] |
59 | 17 | ``` |
60 | 18 |
|
61 | 19 |
|
0 commit comments