forked from nrkapri/JavaAdvancedTopics
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamExamples.java
More file actions
141 lines (96 loc) · 3.09 KB
/
Copy pathStreamExamples.java
File metadata and controls
141 lines (96 loc) · 3.09 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package java8;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String toString()
{
return name+", "+age;
}
}
public class StreamExamples {
public static void main (String [] args)
{
List<String> input = Arrays.asList("Nayan", "Chayan", "Ayan", "Sayan");
List<String> result = input.stream()
.filter((name ) -> name.contains("ayan"))
.collect(Collectors.toList());
result.forEach((x)->System.out.println(x+ ", "));
List<Person> persons = Arrays.asList(
new Person("mkyong", 30),
new Person("Song", 20),
new Person("Jong", 40),
new Person("Young", 40),
new Person("monk", 40),
new Person("Sunk", 40)
);
persons.stream()
.filter((p)-> {return p.getAge()>25;})
.forEach( (p)->System.out.println(p.toString()));
Person onk = persons.stream()
.filter(x -> x.getName().contains("onk"))
.findAny()
.orElse(null);
System.out.println("Onk 1:"+onk.toString());
long count = persons.stream()
.filter(x -> x.getAge()>30)
.map(x -> x.getName().toUpperCase())
.count();
System.out.println("Countis :"+count);
persons.stream()
.filter(x -> x.getAge()>30)
.map(x -> x.getName().toUpperCase())
.forEach(x->System.out.println(x));
//Convert a Stream to List
Stream<String> language = Stream.of("java", "python", "node");
//Convert a Stream to List
List<String> result1 = language.collect(Collectors.toList());
result1.forEach(System.out::println);
//Example – Stream is closed!
String[] array = {"a", "b", "c", "d", "e"};
Stream<String> stream = Arrays.stream(array);
// loop a stream
stream.forEach(x -> System.out.println(x));
// reuse it to filter again! throws IllegalStateException
//long count1 = stream.filter(x -> "b".equals(x)).count();
//System.out.println(count1);
//Stream reuse
Supplier<Stream<String>> streamSupplier = () -> Stream.of(array);
//get new stream
streamSupplier.get().forEach(x -> System.out.println(x));
//get another new stream
long count1 = streamSupplier.get().filter(x -> "b".equals(x)).count();
System.out.println(count1);
//Check if Array contains a certain value?
String[] alphabet = new String[]{"A", "B", "C"};
boolean resultx = Arrays.stream(alphabet).anyMatch("A"::equals);
if (resultx) {
System.out.println("Hello A");
}
int[] number = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
//Java 8
boolean resulty = IntStream.of(number).anyMatch(x -> x == 4);
if (resulty) {
System.out.println("Hello 4");
} else {
System.out.println("Where is number 4?");
}
}
}