본 게시물은 스스로의 공부를 위한 글입니다.
틀린 내용이 있을 수 있습니다.
list.stream() //스트림 만들기
.distinct() //중간연산
.limit(5) //중간연산
.sorted() //중간연산
.forEach(System.out::println) //최종연산
lterator처럼 일회용이다. (필요하면 다시 스트림을 생성해야 함).parallel)IntStream, LongStream, DoubleStream등 제공Stream<Integer> 대신에 IntStream을 사용하는게 더 효율적이다.Stream<T>보다 더 많이 제공한다.(.sum(), .averge() 등).stream()으로 스트림 변환Stream<Integer> stream = Arrays.asList(1, 2, 3, 4, 5).stream();
Stream.of("a", "b", "c");
Stream.of(new String[]{"a", "b", "c"});
Arrays.stream(new String[]{"a", "b", "c"});
Arrays.stream(new String[]{"a", "b", "c"}, startIndex, EndIndex+1);
//Stream.iterate(T seed, UnaryOperator f); 이전 결과에 종속적
Stream.iterate(0, n->n+2); //0, 2, 4, ... 무한 스트림
//generate는 초기값 지정x, 이전 결과와 무관하다.
Stream.generate(Math::random);
Stream.generate(()->1);
.distinct() //중복제거
.filter(Predicate<T> predicate) //조건에 안 맞는 요소는 제외
.limit(long maxSize) //maxSize 이후의 요소는 잘래냄
.skip(long n) //앞에서부터 n개 건너뛰기
.sorted() //기본 정렬로 정렬
.sorted(Comparator<T> comparator) //조건에 맞게 요소 정렬. 추가 정렬 기준을 제공할 때는 thenComparing()사용
//스트림의 요소를 변환. ex) map(File::getName), map(s->s.subString(3))
.map(Function<T> mapper)
//요소에 작업수행. 보통 중간 작업결과 확인으로 사용. peek(s->System.out.println(s))
.peek(Consumer<T> action)
//스트림의 스트림을 스트림으로 변환
//ex) Stream<String> strStrm=strArrStrm.flatMap(Arrays::stream)
.flatMap()
void forEach(Consumer<? super T> action) //각 요소에 지정된 작업 수행
void forEachOrdered(Consumer<? super T> action) //병렬 스트림의 경우 순서를 유지하며 수행
long count() //스트림의 요소 개수 반환
Optional<T> max(Comparator<? super T> comparator) //스트림의 최대값 반환
Optional<T> min(Comparator<? super T> comparator) //스트림의 최소값 반환
Optional<T> findAny() //아무거나 하나 반환. 벙렬 스트림에 사용
Optional<T> findFirst() //첫 번째 요소 반환. 순차 스트림에 사용
boolean allMatch(Predicate<T> p) //모든 조건을 만족?
boolean anyMatch(Predicate<T> p) //조건을 하나라도 만족?
boolean noneMatch(Predicate<T> p) //모든 조건을 만족하지 않음?
Object[] toArray() //모든 요소를 배열로 반환
A[] toArray(IntFunction<A[]> generator) //특정 타입의 배열로 반환
//스트림의 요소를 하나씩 줄여가면서 계산
//아래에서 자세히 보자
Optional<T> reduce(BinaryOperator<T> accumulator)
//데이터를 변형 등의 처리를 하고 원하는 자료형으로 변환해 줍니다.
//아래에서 자세히 보자.
collect( ~ )
if문이라고 생각하면 될듯.
람다식의 리턴값은 boolean. true면 다음 단계 진행, false면 버려짐
classes.stream()
.filter(c->c.getTitle().startWith("spring"))
.forEach(c->System.out.println(oc.getId));
classes.stream()
.filter(Predicate.not(OnlineClass::isClosed))
.forEach(c->System.out.println(oc.getId));
stream을 우리가 원하는 모양의 새로운 스트림으로 변환
map(File::getName)
map(s->s.subString(3))
Stream.iterate(10, i->i+1)
.skip(10)
.limit(10)
.forEach(System.out::println)
boolean test=javaClasses.stream()
.anyMatch(oc->oc.getTitle().contains("k"));
스트림을 직렬로 처리할 때는 차이가 없다.
하지만 병렬로 처리할 경우에 차이가 생기는데,
findFirst()는 stream의 순서를 고려해, 가장 앞쪽에 있는 요소를 반환findAny()는 멀티 쓰레드에서 가장 먼저 찾은 요소를 반환. stream의 뒤쪽에 있는 요소가 반환될 수도 있다.reduce(초기값, (누적 변수, 요소)-> 수행문)Integer sum = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.reduce((total, y) -> total + y);
System.out.println("sum: " + s); //sum: 55
//초기값을 지정해 줄 수 있다.
Integer sum = Stream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
.reduce(10, (total, n) -> total + n);
System.out.println("sum: " + sum); //sum: 65
stream.collect(Collectors.toSet()); //set으로 변환
stream.collect(Collectors.toList()); //list 변환
stream.collect(Collectors.joining()); //한개의 string으로 변환
stream.collect(Collectors.joining(", ")); //요소들 사이에 ","을 넣어서 한개의 string 반환
잘 보고 갑니다!!