티스토리 뷰

스프링, 자바

자바의 Stream API

killog 2021. 4. 9. 18:44
반응형

Stream

  • sequence of elements supporting sequential and parallel aggregate operations
  • 데이터를 담고 있는 저장소(컬렉션)이 아니다.
  • functional in nature , 스트림이 처리하는 데이터 소스를 변경하지 않느다.
  • 스트림으로 처리하는 데이터는 오직 한 번만 처리한다.
  • 무제한일 수 있다. (Short Circuit 메소드를 사용해 제한가능함)
  • 손쉽게 병렬처리할 수 있다.

스트림 파이프라인

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

중개 오퍼레이션

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

종료 오퍼레이션

  • Stream 을 리턴하지 않는다.
  • collect, allMatch, count, forEach, min, max..

코드 예시

package com.github.prgrms;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamExe {
    public static void main(String[] args) {
        List<String> names = new ArrayList<>();
        names.add("Kil");
        names.add("log");
        names.add("ant");
        names.add("그래도 어느정도 넣어줘야 parallelStream 에서 다른 스레드 사용하는것을 확인할 수 있네요");
        names.add("dev");
        names.add("hope");

        Stream<String> stringStream = names.stream().map(String::toUpperCase).limit(5);
        List<String> collect = names.stream().map((s) -> {
            System.out.println(s);
            return s.toUpperCase();
        }).collect(Collectors.toList());//중개형 오퍼레이터 (map) 의 겨웅 종결형 오퍼레이터 오기전까지 실행 안한다.
        //.collect(Collectors.toList()) : 종결형 오퍼레이터

        System.out.println("🎄🎄🎄🎄🎀🎀🎀🎀🎀🎀stream 적용 확인==========");

        collect.forEach(System.out::println);

        System.out.println("🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈같은 스레드 (stream)");


        List<String> collectSameThread = names.stream().map(
                (s) -> {
                    System.out.println(s + " " + Thread.currentThread().getName());
                    return s.toUpperCase();
                }
        ).collect(Collectors.toList());
        //collectSameThread.forEach(System.out::println);

        System.out.println("✨✨✨✨✨✨✨✨✨다른 스레드 (parallelStream) (병렬처리가 항상 빠른것은 아니다. 주의하자. 데이터가 정말 방대할때 유리한 성능)");

        List<String> collectDifferentThread = names.parallelStream().map(
                (s) -> {
                    System.out.println(s + " " + Thread.currentThread().getName());
                    return s.toUpperCase();
                }
        ).collect(Collectors.toList());
        //   collectDifferentThread.forEach(System.out::println);
    }
    /*
        <--출력 결과-->
        Kil
        log
        ant
        그래도 어느정도 넣어줘야 parallelStream 에서 다른 스레드 사용하는것을 확인할 수 있네요
        dev
        hope
        🎄🎄🎄🎄🎀🎀🎀🎀🎀🎀stream 적용 확인==========
        KIL
        LOG
        ANT
        그래도 어느정도 넣어줘야 PARALLELSTREAM 에서 다른 스레드 사용하는것을 확인할 수 있네요
        DEV
        HOPE
        🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈🎈같은 스레드 (stream)
        Kil main
        log main
        ant main
        그래도 어느정도 넣어줘야 parallelStream 에서 다른 스레드 사용하는것을 확인할 수 있네요 main
        dev main
        hope main
        ✨✨✨✨✨✨✨✨✨다른 스레드 (parallelStream) (병렬처리가 항상 빠른것은 아니다. 주의하자. 데이터가 정말 방대할때 유리한 성능)
        그래도 어느정도 넣어줘야 parallelStream 에서 다른 스레드 사용하는것을 확인할 수 있네요 main
        hope main
        dev main
        log main
        ant main
        Kil main

     */
}

스트림 API 활용해보기

스트림 API 사용 예제

걸러내기

  • Filter(Predicate)
  • 예) 이름이 3글자 이상인 데이터만 새로운 스트림으로

변경하기

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

생성하기

  • generate(Supplier) 또는 iterate(T seed, UnaryOperator)
  • 예1) 10 부터 1씩 증사하는 무제한 숫자 스트림
  • 예2) 랜덤 int 무제한 스트림

제한하기

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

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

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

개수 세기

  • count()
  • 예) 10보다 큰 수의 개수를 센다.

코드 예시

package me.whiteship.java8to11;

public class OnlineClass {

    private Integer id;

    private String title;

    private boolean closed;

    public OnlineClass(Integer id, String title, boolean closed) {
        this.id = id;
        this.title = title;
        this.closed = closed;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public boolean isClosed() {
        return closed;
    }

    public void setClosed(boolean closed) {
        this.closed = closed;
    }
}
package me.whiteship.java8to11;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class App {

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



        System.out.println("spring 으로 시작하는 수업");
        springClasses.stream().filter(c->c.getTitle().startsWith("spring")).forEach(s-> System.out.println(s.getId()));
        System.out.println("close 되지 않은 수업");
        springClasses.stream().filter(s->!s.isClosed()).forEach(a-> System.out.println(a.getId()));
        System.out.println("수업 이름만 모아서 스트림 만들기");
        springClasses.stream().map(OnlineClass::getTitle).forEach(System.out::println);


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

        List<List<OnlineClass>> keesunEvents = new ArrayList<>();
        keesunEvents.add(springClasses);
        keesunEvents.add(javaClasses);


        System.out.println("두 수업 목록에 들어있는 모든 수업 아이디 출력");
        keesunEvents.stream().flatMap(Collection::stream).forEach(o-> System.out.println(o.getId()));

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

        System.out.println("자바 수업 중에 Test가 들어있는 수업이 있는지 확인");
        boolean tEst = javaClasses.stream().anyMatch(o -> o.getTitle().contains("TEst"));
        System.out.println("tEst = " + tEst);
        System.out.println("스프링 수업 중에 제목에 spring이 들어간 것만 모아서 List로 만들기");

        List<OnlineClass> spring = springClasses.stream().filter(o -> o.getTitle().contains("spring")).collect(Collectors.toList());

    }

}

참고 문헌

백기선 강사님 더 자바8 강의

https://docs.google.com/document/d/1UxKM56um1mjGeayxmJmvALM5CDIJC17vx1-sDwoEbSs/edit#heading=h.yxg0ssbn3wpe

반응형

'스프링, 자바' 카테고리의 다른 글

DB Connection Pool  (0) 2021.04.23
Optional  (0) 2021.04.09
AspectJ 과 Spring AOP  (0) 2021.03.29
Log4j 레벨 간단 정리  (0) 2021.03.29
Dispatcher-Servlet  (0) 2021.03.04
댓글
반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
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
글 보관함