-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamEx.java
More file actions
64 lines (50 loc) · 1.7 KB
/
StreamEx.java
File metadata and controls
64 lines (50 loc) · 1.7 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
package com.jin.algo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class StreamEx {
public static void main(String[] args) {
List<String> programing =
Arrays.asList("Javascript", "C", "C++", "Nodejs", "Java", "Oracle", "MariaDB", "PHP", "ASP");
int count1 = 0;
for (String str : programing) {
if(str.indexOf("Java") > -1){
count1 += 1;
}
}
System.out.println("1-1.count1 = " + count1);
long count2 = programing.stream()
.filter(str->str.indexOf("java") > -1)
.count();
System.out.println("1-2.count2 = " + count2);
long count3 = programing.parallelStream()
.filter(str -> str.indexOf("Java") > -1)
.count();
System.out.println("1-3.count3 = " + count3);
// 2.Stream 생성
// 공백을 붙여서 하나의 스트링으로 만듬
String contents = programing.stream()
.collect(Collectors.joining(" "));
// 공백별로 잘라서 String[]배열을 반환
Stream<String> splitStr = Stream.of(contents.split(" "));
System.out.print("\n2.splitStr :");
splitStr.forEach(str -> System.out.print("[" + str + "]"));
System.out.print("\n3-2.map(toUpperCase) :");
programing.stream()
.map(String::toUpperCase)
.forEach(str -> System.out.print("[" + str + "]"));
System.out.print("\n3-3.flatMap() :");
programing.stream()
.filter(str->str.length()==3)
.flatMap(str->{
List<String> result = new ArrayList<>();
for(int i=0; i<str.length(); i++) {
result.add(str.substring(i, i+1));
}
return result.stream();
})
.forEach(str->System.out.println("["+str+"]"));
}
}