-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyStreamV3.java
More file actions
51 lines (42 loc) · 1.25 KB
/
Copy pathMyStreamV3.java
File metadata and controls
51 lines (42 loc) · 1.25 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
package lambda.lambda5.mystream;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
// static factory 추가
public class MyStreamV3<T> {
List<T> integerList;
private MyStreamV3(List<T> integerList) {
this.integerList = integerList;
}
// static factory
public static <T> MyStreamV3<T> of(List<T> integerList) {
return new MyStreamV3<>(integerList);
}
public MyStreamV3<T> filter (Predicate<T> predicate){
List filtered = new ArrayList<>();
for (T element : integerList) {
if(predicate.test(element)) filtered.add(element);
}
return MyStreamV3.of(filtered);
}
public <R> MyStreamV3<R> map (Function<T, R> mapper) {
List<R> mapped = new ArrayList<>();
for (T integer : integerList) {
mapped.add(mapper.apply(integer));
}
return MyStreamV3.of(mapped);
}
public List<T> toList(){
return integerList;
}
public void forEach(Consumer<T> consumer){
for (T element : integerList) {
consumer.accept(element);
}
}
public T getFirst(){
return integerList.get(0);
}
}