-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreateStreamMain.java
More file actions
36 lines (32 loc) · 1.42 KB
/
Copy pathCreateStreamMain.java
File metadata and controls
36 lines (32 loc) · 1.42 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
package stream.operation;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class CreateStreamMain {
public static void main(String[] args) {
System.out.println("1. 컬랙션으로부터 생성");
List<String> list = List.of("a", "b", "c");
Stream<String> stream1 = list.stream();
stream1.forEach(System.out::println);
System.out.println();
System.out.println("2. 배열로부터 생성");
String[] arr = {"a", "b", "c"};
Stream<String> stream2 = Arrays.stream(arr);
stream2.forEach(System.out::println);
System.out.println();
System.out.println("3. Stream.of() 사용");
Stream<String> stream3 = Stream.of("a", "b", "c");
stream3.forEach(System.out::println);
System.out.println();
System.out.println("4. 무한 스트림 생성 - iterate()");
// iterate: 초기값과 다음 값을 만드는 함수 지정
Stream<Integer> infiniteStream = Stream.iterate(0, i -> i +2);
infiniteStream.limit(3).forEach(System.out::println);
System.out.println();
System.out.println("5. 무한 스트림 생성 - generate()");
// generate: Supplier를 사용하여 무한하게 생성
Stream<Double> randomStream = Stream.generate(Math::random);
randomStream.limit(3).forEach(System.out::println);
}
}