stream()
으로 컬렉션을 스트림으로 변환stream()
을 쓸 수 있다.
Stream<E> stream()
ex14_05_1
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;
public class Ex14_05_1 {
public static void main(String[] args) {
List<Integer> list = Arrays.asList(1,2,3,4,5);
Stream<Integer> intStream = list.stream(); //list를 Stream으로
intStream.forEach(System.out::print);
// intStream.forEach(System.out::print); //에러:intStream closed
//스트림은 일회용이고, forEach()는 최종연산이므로 닫힘!!
//**다시 생성해야 한다!!!!
intStream = list.stream();
intStream.forEach(System.out::print);
}
}
1234512345
Stream.of()
Stream<T> Stream.of(T... values)
Stream<T> Stream.of(T[])
Arrays.stream(T[])
Arrays
클래스에도 stream()
이 있다!!!
Stream<T> Arrays.stream(T[])
Stream<T> Arrays.stream(T[] array, int startInclusive, int endExclusive)
IntStream.of()
IntStream IntStream.of(int... values)
IntStream IntStream.of(int[])
Arrays.stream()
IntStream Arrays.stream(int[])
IntStream Arrays.stream(int[] array, int startInclusive, int endExclusive)
ex14_05_1
import java.util.Arrays;
import java.util.List;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Ex14_05_1 {
public static void main(String[] args) {
//2. 배열을 스트림으로
//1)Stream.of(가변인자)
Stream<String> strStream = Stream.of("a","b","c");
strStream.forEach(System.out::print);
System.out.println();
//2)Stream.of(T t)
Stream<String> strStream2 = Stream.of(new String[] {"a","b","c"});
strStream2.forEach(System.out::print);
System.out.println();
//위는 아래 두줄을 줄인 것
String[] strArr = {"a","b","c"};
Stream<String> strStream3 = Stream.of(strArr);
strStream3.forEach(System.out::print);
System.out.println();
//3)Arrays.stream(T[])
Stream<String> strStream4 = Arrays.stream(strArr);
strStream4.forEach(System.out::print);
System.out.println();
int[] intArr = {1,2,3,4,5};
IntStream intStream2 = Arrays.stream(intArr);
// intStream2.forEach(System.out::print);
// System.out.println(intStream2.count());
// System.out.println(intStream2.sum());
System.out.println("average="+intStream2.average()); //다 최종연산이라 재생성 필수!!
//Stream<Integer>로 하고 싶으면
Integer[] intArr2 = {1,2,3,4,5};
Stream<Integer> intStream3 = Arrays.stream(intArr2);
// intStream3.forEach(System.out::print);
System.out.println("count="+intStream3.count()); //이것밖에 없음!!!
}
}
abc
abc
abc
abc
//12345
//count=5
//sum=15
average=OptionalDouble[3.0]
count=5
난수를 요소로 갖는 스트림 생성하기
Random
클래스의 메소드들 : ints()
, longs()
, doubles()
-> 해당 범위에 있는 난수&무한(또는 유한) 스트림 생성
limit(숫자)
: 무한 스트림이면 반드시 잘라야 한다. 제한을 안두면 끝도 없이 나옴ex14_05_2
import java.util.Random;
import java.util.stream.IntStream;
public class Ex14_05_2 {
public static void main(String[] args) {
IntStream intStream = new Random().ints(); //무한스트림
intStream.limit(5).forEach(System.out::println); //limit()필수!!
IntStream intStream3 = new Random().ints(3, 10); //3~9
intStream3.limit(5).forEach(System.out::println); //limit()필수!!
IntStream intStream2 = new Random().ints(5);//유한스트림
intStream2.forEach(System.out::println);
IntStream intStream4 = new Random().ints(5, 3, 10); //3~9
intStream4.forEach(System.out::println);
}
}
-468109592
631298685
265823285
-819366561
-2089176965
5
9
4
9
7
1824864387
382440383
-1628628531
-1553195391
-1332646964
9
7
3
5
3
IntStream
, LongStream
IntStream IntStream.range(int begin, int end) //begin ~ end-1 IntStream IntStream.rangeClosed(int begin, int end) //begin ~ end포함!!
예)
IntStream intStream = IntStream.range(1, 5); //1,2,3,4
IntStream intStream = IntStream.rangeClosed(1, 5); //1,2,3,4,5
iterate()
static <T> Stream<T> iterate(T seed, UnaryOperator<T> f)
T seed
: 초깃값UnaryOperator<T> f
: 단항 연산자 람다식IntStream
..)의 참조변수로 다룰 수 없다.예)
Stream<Integer> evenStream = Stream.iterate(0, n->n+2);
//0, 2, 4, 6, ... 무한!!!
generate()
static <T> Stream<T> generate(Supplier<T> s)
Supplier<T> s
: 입력X, 출력O 람다식IntStream
..)의 참조변수로 다룰 수 없다.예)
Stream<Double> randomStream = Stream.generate(Math::random);
//() -> Math.random()
Stream<Integer> oneStream = Stream.generate(() -> 1);
//1, 1, 1, ...
ex14_05_3
import java.util.stream.Stream;
public class Ex14_05_3 {
public static void main(String[] args) {
// 람다식 스트림 만들기
//iterate(T seed, UnaryOperator f) 단항연산자
Stream<Integer> intStream = Stream.iterate(0, n -> n + 2);
intStream.limit(5).forEach(System.out::println);
//generate(Supplier s) 입력X 출력O
Stream<Integer> oneStream = Stream.generate(() -> 1);
oneStream.limit(5).forEach(System.out::println);
}
}
0
2
4
6
8
1
1
1
1
1
Stream<String>
으로 만듦Stream emptyStream = Stream.empty(); long count = emptyStream.count(); //count = 0
empty()
: 빈 스트림을 생성해서 반환