-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathSortUsingStreams.java
More file actions
29 lines (23 loc) · 899 Bytes
/
SortUsingStreams.java
File metadata and controls
29 lines (23 loc) · 899 Bytes
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
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
public class SortUsingStreams {
public static void main(String[] args) {
List<Integer> numbers =
Arrays.asList(5, 3, 9, 1, 6, 2);
List<Integer> sortedNumbersDesc =
numbers.stream()
.sorted(Comparator.reverseOrder())
.toList();
System.out.println("Sorted numbers in descending order: " +
"" + sortedNumbersDesc);
List<String> words =
Arrays.asList("apple", "banana", "kiwi", "cherry");
List<String> sortedWordsByLength = words.stream()
.sorted(Comparator.comparingInt(String::length).reversed())
.toList();
System.out.println("Sorted words by length: "
+ sortedWordsByLength);
}
}