forked from Rapter1990/JavaStreamAPIExamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSortedDistinctExample.java
More file actions
43 lines (33 loc) · 1.33 KB
/
SortedDistinctExample.java
File metadata and controls
43 lines (33 loc) · 1.33 KB
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
37
38
39
40
41
42
43
package functionalexamples;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class SortedDistinctExample {
public static void main(String[] args) {
List<String> names = Arrays.asList("Sergio","Sunil","Paul","Ana","Michelle",
"Sunil","Sergio","Anita");
List<String> sortedNames = fetchLongNamesSorted(names);
System.out.println("Names sorted :"+sortedNames);
List<String> sortedDistinctNames = fetchDistinctLongNamesUpper(names);
System.out.println("Distinct names sorted :"+sortedDistinctNames);
}
/**
* Example showing sorted method on stream.Filter names with length >3 and
* sort them.
* @param names - List of names
* @return - List of names having length > 3 and sorted alphabetically.
*/
public static List<String> fetchLongNamesSorted(List<String> names) {
return names.stream()
.filter(name -> name.length() > 3)
.sorted()
.collect(Collectors.toList());
}
public static List<String> fetchDistinctLongNamesUpper(List<String> names) {
return names.stream()
.filter(name -> name.length() > 3)
.distinct()
.map(name -> name.toUpperCase())
.collect(Collectors.toList());
}
}