Stream 소개

Stream이란 용어가 익숙하지 않을 수 있다. Stream이란 컨베이너 벨트에 데이터를 놓고 컨베이너 벨트를 지나가며 원하는 형식에 따라 데이터 처리를 하는 것이라고 보면 된다.

 

Stream

  • sequence of elements supporting sequential and parallel aggregate operations
  • 데이터를 담고 있는 저장소(컬렉션)가 아니다.
  • Functional in nature, 스트림이 처리하는 데이터 소스를 변경하지 않는다. (기존 데이터는 그대로라고 보면 된다.)
  • 스트림으로 처리하는 데이터는 오직 한번만 처리한다.
  • 무제한일 수도 있다. (Short Circuit 메소드를 사용해서 제한할 수 있다.)
  • 중개 오퍼레이션은 근본적으로 lazy하다. (lazy 함은 종료 오퍼레이션이 오기전까지, 작업을 수행하지 않는다. 라고 보면 좋다.)
  • 손쉽게 병렬 처리 할 수 있다.

 

스트림 파이프라인

  • 0 또는 다수의 중개 오퍼레이션 (intermediate operation)과 한개의 종료 오퍼레이션 (terminal operation으로 구성한다.
  • 스트림의 데이터 소스는 오직 터미널 오퍼레이션을 실행할 때에만 처리한다.

 

중개 오퍼레이션

  • Stream을 리턴한다.
  • Stateless / Stateful 오퍼레이션으로 더 상세하게 구분할 수도 있다. (대부분은 Stateless지만 distinct나 sorted 처럼 이전 소스 데이터를 참조해야 하는 오퍼레이션은 Stateful 오퍼레이션이다.)
  • filter, map, limit, skip, sorted, ....

 

종료 오퍼레이션

  • Stream을 리턴하지 않는다. (Stream을 다른 자료구조로 변경하기 때문)
  • collect, allMatch, count, forEach, min, max, ....

 

이제 코드로 확인해보자.

package me.qazyj.java8;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamTest {

    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        names.add("kyj");
        names.add("yj");
        names.add("kim");
        names.add("young_jin");

        Stream<String> stringStream = names.stream().map(String::toUpperCase);
        names.forEach(System.out::println); // 기존 데이터는 소문자 그대로이다.

        System.out.println("==============");

        // 중개 오퍼레이션이 lazy함의 증거로 아무출력도 일어나지 않는다.
        names.stream().map((s) -> {
            System.out.println(s);
            return s.toUpperCase();
        });

        // 중개 오퍼레이션 1개와 종료 오퍼레이션 1개로 stream 처리한 예시
        // 종료 오퍼레이션이 왔기때문에 출력이 발생한다.
        // 종료 오퍼레이션은 아래와 같이 Stream이 아닌 List나 Set과 같은 자료형임을 볼 수 있다.
        List<String> collect = names.stream().map((s) -> {
            System.out.println(s);
            return s.toUpperCase();
        }).collect(Collectors.toList());

        // 병렬 처리 예시
        List<String> collect1 = names.parallelStream().map(String::toUpperCase)
                .collect(Collectors.toList());
        collect1.forEach(System.out::println);
    }
}

병렬 처리할 때 사용하는 parallelStream의 경우는 오히려 성능을 저하시킬 수 있다. 상황에 따라서 병렬로 하는게 빠를 수 있고, 더 느릴 수 있기 때문에 병렬을 쓸 때와 쓰지 않았을 때의 시간 차이를 보고 적절한 방법으로 사용하는걸 권장한다.

 

병렬처리 테스트에 관련한 자료는 아래

 

[JAVA 8] ParallelSort

Arrays.parallelSort() Fork/Join 프레임워크를 사용해서 배열을 병렬로 정렬하는 기능을 제공한다. 병렬 정렬 알고리즘 배열을 둘로 계속 쪼갠다. 합치면서 정렬한다. 이전에 parallelStream을 설명할 때 말

qazyj.tistory.com

 

 

 

Stream API

스트림 API 사용 예시

 

걸러내기

  • Filter (Predicate)
  • 예) 문자열 시작이 kim인 데이터만 새로운 스트림으로

 

변경하기 

  • Map(Function) 또는 FlatMap(Function)
  • 예) 각각의 User 인스턴스에서 String name만 새로운 스트림으로 
  • 예) List<Stream<String>>을 String의 스트림으로

 

생성하기

  • Generate(Supplier) 또는 Iterate(T seed, UnaryOperator)
  • 예) 2씩 증가하는 무제한 숫자 스트림
  • 예) 랜덤 Integer 무제한 스트림 (limit을 주지 않으면 무제한으로 간다.)

 

제한하기

  • limit(long) 또는 skip(long)
  • 예) 최대 5개의 요소가 담긴 스트림을 리턴한다.
  • 예) 앞에서 3개를 뺀 나머지 스트림을 리턴한다.

스트림에 있는 데이터가 특정 조건을 만족하는지 확인

  • anyMatch(), allMatch(), nonMatch()
  • 예) k로 시작하는 문자열이 있는지 확인한다. (true 또는 false return)
  • 예) 스트림에 있는 모든 문자열의 길이가 10보다 작은지 확인한다.

 

개수 세기

  • count()
  • 예) k로 시작하는 문자열의 개수를 센다.

 

스크림을 데이터 하나로 뭉치기

  • reduce(identity, BiFunction), collect(), sum(), max
  • 예) 모든 숫자 합 구하기
  • 예) 모든 데이터를 하나의 List 혹은 Set에 옮겨 담기

이제 코드로 알아보자.

 

package me.qazyj.java8;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class OnlineClassTest {

    public static void main(String[] args) {
        List<OnlineClass> springClass = new ArrayList<>();
        springClass.add(new OnlineClass(1, "spring boot", true));
        springClass.add(new OnlineClass(2, "spring data jpa", true));
        springClass.add(new OnlineClass(3, "spring mvc", false));
        springClass.add(new OnlineClass(4, "spring core", false));
        springClass.add(new OnlineClass(5, "spring batch", true));
        springClass.add(new OnlineClass(6, "rest api", true));

        System.out.println("spring 문자열로 시작하는 OnlineClass의 id");
        springClass.stream().filter(oc -> oc.getTitle().startsWith("spring"))
                        .forEach(oc -> System.out.println(oc.getId()));
        System.out.println();

        System.out.println("close 되지 않은 OnlineClass의 id");
        springClass.stream().filter(oc -> !oc.getClosed())   // Predicate.not(OnlineClass::getClosed()) 도 가능
                        .forEach(oc -> System.out.println(oc.getId()));
        System.out.println();

        System.out.println("수업 이름만 모아서 스트림 만들기");
        springClass.stream().map(OnlineClass::getTitle)
                        .forEach(System.out::println);
        System.out.println();


        List<OnlineClass> javaClass = new ArrayList<>();
        javaClass.add(new OnlineClass(7, "The Java, Test", true));
        javaClass.add(new OnlineClass(8, "The Java, Code", true));
        javaClass.add(new OnlineClass(9, "The Java, 8 to 11", false));

        List<List<OnlineClass>> kyjEvents = new ArrayList<>();
        kyjEvents.add(springClass);
        kyjEvents.add(javaClass);


        System.out.println("두 수업 목록에 들어있는 모든 OnlineClass의 id");
        // flatMap이란 List 등과 같은 컬렉션의 데이터들을 풀어 놓는 것?? 이라고 보면된다. 현재는 OnlineClass 하나 하나 데이터들이 나오게 된다고 보면 된다.
        kyjEvents.stream().flatMap(Collection::stream)      // list -> stream 으로 flat
                        .forEach(oc -> System.out.println(oc.getId()));
        System.out.println();

        System.out.println("2씩 증가하는 무제한 스트림 중 앞에 2개 뺴고 최대 10개까지만");
        Stream.iterate(0, i -> i + 2)
                .skip(2)
                .limit(10)
                .forEach(System.out::println);
        System.out.println();

        System.out.println("자바 수업 중에서 Test가 들어있는 수업이 있는지 확인");
        boolean test = javaClass.stream().anyMatch(oc -> oc.getTitle().contains("Test"));
        System.out.println(test);

		//.collect(Collectors.toList()) 대신 count()를 쓰면 개수가 나옴
        System.out.println("스프링 수업 중에서 제목에 spring이 들어간 것만 모아서 List로 만들기");
        List<String> spring = springClass.stream().filter(oc -> oc.getTitle().contains("spring"))
                .map(OnlineClass::getTitle)
                .collect(Collectors.toList());
        spring.forEach(System.out::println);
        System.out.println();


    }
}

 

 

 

 

 

 

'JAVA' 카테고리의 다른 글

[Java 8] Date와 Time  (0) 2022.10.12
[Java 8] Optional  (0) 2022.10.07
[JAVA 8] 인터페이스의 변화  (0) 2022.10.06
[JAVA 8] 함수형 인터페이스와 람다 표현식  (0) 2022.09.23
ListIterator  (0) 2022.09.23

+ Recent posts