-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathApp.java
More file actions
87 lines (63 loc) · 2.43 KB
/
App.java
File metadata and controls
87 lines (63 loc) · 2.43 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.hmkcode;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class App
{
public static void main( String[] args )
{
String[] arr = new String[]{"a", "b", "c", "d"};
Stream<String> stream = Arrays.stream(arr);
stream = Stream.of("a", "b", "c", "d");
List<String> list = new LinkedList<String>();
list.add("a");
list.add("b");
stream = list.stream();
// forEach()
stream = Stream.of("a", "b", "c", "d");
stream.forEach(e -> System.out.println(e));
// distinct() | count()
stream = Stream.of("a", "b", "c", "d");
System.out.println(stream.distinct().count());
// anyMatch()
stream = Stream.of("a", "b", "c", "d");
System.out.println(stream.anyMatch(e -> e.contains("a")));
// filter()
stream = Stream.of("a", "b", "c", "d");
stream.filter(e -> e.contains("b")).forEach(e -> System.out.println(e));
// map()
stream = Stream.of("a", "b", "c", "d");
stream.map(e -> e.toUpperCase()).forEach(e -> System.out.println(e));
// flatMap()
stream = getBigList().stream().flatMap(lst -> lst.stream());
stream.forEach(e -> System.out.println(e));
//[any|all|none]Match()
System.out.println(Stream.of("a", "b", "c", "d").allMatch( e -> (e.length() == 1)));
System.out.println(Stream.of("a", "b", "c", "d").noneMatch(e -> (e.length() == 2)));
System.out.println(Stream.of("a", "b", "c", "d").anyMatch( e -> e.equals("a") ));
//reduce()
stream = Stream.of("a", "b", "c", "d");
System.out.println(stream.reduce("", (x,y) -> apply(x,y)));
//collect(Collectors)
stream = Stream.of("a", "b", "c", "d");
System.out.println(stream.collect(Collectors.toList()));
}
private static String apply(String a, String b){
System.out.println(a+"->"+b);
return a+b;
}
private static List<List<String>> getBigList(){
List<List<String>> bigList = new LinkedList<List<String>>();
List<String> list1 = new LinkedList<String>();
list1.add("a");
list1.add("b");
List<String> list2 = new LinkedList<String>();
list2.add("c");
list2.add("d");
bigList.add(list1);
bigList.add(list2);
return bigList;
}
}