14. 람다와 스트림

메밀·2022년 11월 9일
0

자바의 정석

목록 보기
6/6

1. 람다

1) 람다식이란?

메서드를 하나의 식으로 표현한 것
익명함수

Arrays.setAll(arr, (i)(int)(Math.random() * 5 ) + 1);

2) 람다식 작성하기

int max(int a, int b){
		return a > b ? a : b;
}

(int a, int b) -> {
		return a > b ? a : b;
}

반환값이 있는 메서드의 경우 return문 대신 식으로 대체 가능.
이때는 문장이 아닌 식이므로 ;를 붙이지 않는다

(int a, int b) - > a > b ? a : b

추론 가능한 경우 매개변수의 타입 생략 가능

(a, b) - > a > b ? a : b

선언된 매개변수가 하나뿐일 경우 괄호도 생략 가능
단, 매개변수의 타입이 있으면 괄호 생략 불가

a -> a * a
(int a) -> a * a

— 예시

void printVar(String name, int i){
	sout(name + " = " +i);
}

(name, i) -> sout(name + " = " + i)

////////////

int roll(){
		return (int)(Math.random() * 6);
}

() -> (int)(Math.random() * 6)

////////

int sumArr(int[] arr){
		int sum = 0;
		for(int i : arr)
				sum += i;
		return sum;
}

(int[] arr) -> {
		int sum = 0;
		for(int i : arr)
				sum += i;
		return sum;
}

3) 함수형 인터페이스

람다를 다루기 위한 인터페이스
람다식은 익명클래스의 객체와 동등

함수형 인터페이스에는 오직 하나의 추상 메서드만 정의되어 있어야 함. 그래야 람다식과 인터페이스 메서드가 1:1로 연결될 수 있기 때문. (static, default 메서드의 개수에는 제약이 없다)

interface MyFunction{
    public abstract int max(int a, int b);
}

class Lab{
    public static void main(String[] args) {
        MyFunction f = (int a, int b) -> a > b ? a : b;
        int big = f.max(5, 3); // 익명 객체의 메서드 호출
        System.out.println(big);
    }
}

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

class Lab{
    public static void main(String[] args) {
        List<String> list = Arrays.asList("abc", "aaa", "bbb", "ddd", "aaa");
        Collections.sort(list, (s1, s2) -> s2.compareTo(s1));
        System.out.println(list); // [ddd, bbb, abc, aaa, aaa]
    }
}

— 함수형 인터페이스 타입의 매개변수와 반환타입

함수형 인터페이스 MyFunction이 아래와 같이 정의되어 있을 때,

@FunctionalInterface
interface MyFunction{
    void myMethod();
}

메서드의 매개변수가 MyFunction이면, 이 메서드를 호출할 때 람다식을 참조하는 참조변수를 매개변수로 지정해야 한다는 뜻이다.

void aMethod(MyFunction f){ // 매개변수의 타입이 함수형 인터페이스
		f.myMethod(); // MyFunction에 정의된 메서드 호출
}

MyFunction f = () -> System.out.println("myMethod()");
aMethod(f);

또는 참조변수 없이 아래와 같이 직접 람다식을 매개변수로 지정하는 것도 가능

aMethod( () -> System.out.println("myMethod()"));
// 람다식을 매개변수로 지정

그리고 메서드의 반환타입이 함수형 인터페이스라면 이 함수형 인터페이스의 추상메서드와 동등한 람다식을 가리키는 참조변수를 반환하거나 람다식을 직접 반환할 수 있다.

MyFunction myMethod() {
		return () -> {};
}

람다식을 참조변수로 다룰 수 있다는 것은 메서드를 통해 람다식을 주고받을 수 있다는 것을 의미한다.
즉, 변수처럼 메서드를 주고받는 것이 가능해진 것이다.

@FunctionalInterface
interface MyFunction{
    void run(); // public abstract void run()
}

class Lab{
    static void execute(MyFunction f){
        f.run();
    }

    static MyFunction getMyFunction(){
        return () -> System.out.println("f3.run()");
    }

    public static void main(String[] args) {
        // 람다식으로 MyFunction의 run()을 구현
        MyFunction f1 = () -> System.out.println("f1.run()");
        MyFunction f2 = new MyFunction() {
            @Override
            public void run() {
                System.out.println("f2.run()");
            }
        };

        MyFunction f3 = getMyFunction();

        f1.run(); // f1.run()
        f2.run(); // f2.run()
        f3.run(); // f3.run()

        execute(f1); // f1.run()
        execute( () -> System.out.println("run()")); // run()
    }
}

— 람다식의 타입과 형변환

함수형 인터페이스로 람다식을 참조할 수 있는 것일 뿐, 람다식의 타입이 함수형 인터페이스의 타입과 일치하는 것은 아니다.

람다식은 익명 객체고 익명 객체는 타입이 없다.

(정확히는 타입은 있지만 컴파일러가 임의로 이름을 정하기 때문에 알 수 없다)

그래서 대입 연산자의 양변의 타입을 일치시키기 위하여 아래와 같이 형변환이 필요하다.

MyFunction f = (MyFunction)( () -> {} );

그리고 이 형변환은 생략 가능하다.

— 외부 변수를 참조하는 람다식

@FunctionalInterface
interface MyFunction{
    void myMethod();
}

class Outer{
    int val = 10; // Outer.this.val

    class Inner{
        int val = 20; // this.val

        void method(int i){ // void method(final int i)
            int val = 30;
            // i = 10; // error. 상수의 값 변경 불가

            MyFunction f = () -> {
                System.out.println("i: " + i);
                System.out.println("val: " + val);
                System.out.println("this.val: " + ++this.val);
                System.out.println("Outer.this.val: " + ++Outer.this.val);
            };

            f.myMethod();
        }
    } // end for Inner
} // end for Outer

class Lab{
    public static void main(String[] args) {
        Outer outer = new Outer();
        Outer.Inner inner = outer.new Inner();
        inner.method(100);
    }
}

람다식 내에서 참조하는 지역변수는 final이 붙지 않았어도 상수로 간주된다

람다식 내에서 지역변수 i와 val을 참조하고 있으므로 람다식 내에서나 다른 어느 곳에서도 이 변수들의 값을 변경하는 일은 허용되지 않는다.

반면, Inner클래스와 Outer클래스의 인스턴스 변수인 this.val과 Outer.this.val은 상수로 간주되지 않으므로 값을 변경해도 된다.

cf) 외부 지역변수와 같은 이름의 람다식 매개변수는 허용되지 않는다.

4) java.util.function 패키지

일반적으로 자주 쓰이는 형식의 메서드를 함수형 인터페이스로 미리 정의해놓았다.

함수형 인터페이스메서드설명
java.lang.Runnablevoid run()매개변수도 없고, 반환값도 없음
SupplierT get()매개변수는 없고 반환값만 있음
Consumervoid accept(T t)Supplier와 반대로 매개변수만 있고 반환값이 없음
Function<T, R>R apply(T t)일반적인 함수. 하나의 매개변수를 받아 결과를 반환
Predicateboolean Test(T t)조건식을 표현하는데 사용됨.
매개변수는 하나, 반환타입은 boolean

T: Type
R: Return Type

— 조건식의 표현에 사용되는 Predicate

조건식을 람다식으로 표현하는데에 사용

import java.util.function.Predicate;

class Lab{
    public static void main(String[] args) {
        Predicate<String> isEmptyStr = s -> s.length() == 0;
        String s = "";
        
        if(isEmptyStr.test(s))
            System.out.println("This is an empty String.");
    }
}

— 매개변수가 두 개인 함수형 인터페이스

이름 앞에 접두사 ‘Bi’가 붙는다

함수형 인터페이스메서드설명
BiConsumer<T, U>void accept(T t, U u)두 개의 매개변수만 있고 반환값이 없음
BiPredicate<T, U>boolean test(T t , U u)조건식을 표현하는데 사용됨.
매개변수는 둘, 반환값은 boolean
BiFunction<T, U, R>R apply(T t, U u)두 개의 매개변수를 받아서 하나의 결과를 반환

두 개 이상의 매개변수를 갖는 함수형 인터페이스가 필요하다면 직접 만들어 써야한다.

— UnaryOperator와 BinaryOperator

매개변수의 타입과 반환타입의 타입이 모두 일치한다는 점만 제외하고는 Funtion과 같다.

cf) UnaryOperator와 BinaryOperator의 조상은 각각 Funtion, BiFunction

함수형 인터페이스메서드설명
UnaryOperatorT apply(T t)Function의 자손
Funtion과 달리 매개변수와 결과의 타입이 같다
BinaryOperatorT apply(T t, T t)BiFunction의 자손
매개변수와 결과 타입 같음

— 컬렉션 프레임워크와 함수형 인터페이스

컬렉션 프레임워크 인터페이스의 디폴트 메서드 중 함수형 인터페이스를 사용하는 것들

메서드설명
Collection boolean removeAll(Predicate filter)조건에 맞는 요소를 삭제
List void replaceAll(UnaryOperator operator)모든 요소를 변환하여 대체
Iterable void forEach(Consumer action)모든 요소에 작업 action을 수행
Map V compute(K key, BiFunction<K, V, V> f)지정된 key의 값에 작업 f를 수행
V computeAbsent(K key, Function<K, V> f)키가 없으면 작업 f 수행 후 추가
V computeIfPresent(K key, BiFunction<K, V, V> f)지정된 키가 있을 때 작업 f 수행
V merge(K key, V value, BiFuntion<V, V, V> f)모든 요소에 병합작업 f를 수행
void forEach(BiConsumer<K, V> action)모든 요소에 작업 action을 수행
void replaceAll(BiFunction<K, V, V> f)모든 요소에 치환작업 f를 수행
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Predicate;

class Lab {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            list.add(i);
        }

        // list의 모든 요소를 출력
        list.forEach(i -> System.out.print(i + ", ")); // 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 
        System.out.println();

        // list에서 2 또는 3의 배수를 제거
        list.removeIf(x -> x % 2 == 0 || x % 3 == 0); // [1, 5, 7]
        System.out.println(list);

        list.replaceAll(i -> i * 10); // list의 각 요소에 10을 곱한다 // [10, 50, 70]
        System.out.println(list);

        Map<String, String> map = new HashMap<>();
        for (int i = 0; i < 5; i++) {
            map.put(i + "", i + "");
        }

        // map의 모든 요소를 {k, v} 형태로 출력한다
        map.forEach((k, v) -> System.out.println("{" + k + ", " + v + "}, "));
        System.out.println();

        /*{0, 0}, 
           {1, 1}, 
            {2, 2}, 
            {3, 3}, 
            {4, 4}, */
    }
}

함수형 인터페이스를 사용하는 예제

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;

class Lab{
    public static void main(String[] args) {
        Supplier<Integer> s = () -> (int)(Math.random() * 100) + 1;
        Consumer<Integer> c = i -> System.out.print(i + ", ");
        Predicate<Integer> p = i -> i % 2 == 0;
        Function<Integer, Integer> f = i -> i / 10 * 10; // i의 1의 자리를 없앤다.

        List<Integer> list = new ArrayList<>();
        makeRandomList(s, list);
        System.out.println(list); // [81, 6, 71, 98, 100, 95, 24, 71, 70, 32]

        printEventNum(p, c, list); // [22, 74, 10]
        List<Integer> newList = doSomething(f, list);
        System.out.println(newList); // [20, 40, 70, 30, 0, 20, 10, 70, 0, 10]
    }

    static <T> List<T> doSomething(Function<T, T> f, List<T> list){
        List<T> newList = new ArrayList<>(list.size());

        for(T i : list){
            newList.add(f.apply(i));
        }

        return newList;
    }

    static <T> void printEventNum(Predicate<T> p, Consumer<T> c, List<T> list){
        System.out.print("[");

        for(T i : list){
            if(p.test(i))
                c.accept(i);
        }

        System.out.println("]");
    }

    static <T> void makeRandomList(Supplier<T> s, List<T> list){
        for(int i = 0; i < 10; i++){
            list.add(s.get());
        }
    }
}

— 기본형을 사용하는 함수형 인터페이스

지금까지의 함수형 인터페이스는 매개변수와 반환값의 타입이 모두 지네릭 타입
기본형의 타입을 처리할 때도 래퍼 클래스를 사용
보다 효율적으로 처리할 수 있도록 기본형 함수형 인터페이스도 제공됨

인터페이스메서드설명
DoubleToIntFunctionintapplyAsInt(double d)AtoBFunction은 입력이 A타입, 출력이 B타입
ToIntFunctionint applyAsInt(T value)ToBFunction은 출력이 B타입, 입력은 지네릭 타입
IntFunctionR apply(T t , U u)AFunction은 입력이 A타입, 출력은 지네릭 타입
ObjIntConsumervoid accept(T t, U u)ObjAFunction은 입력이 T, A 타입이고 출력은 없다
import java.util.Arrays;
import java.util.function.IntConsumer;
import java.util.function.IntPredicate;
import java.util.function.IntSupplier;
import java.util.function.IntUnaryOperator;

class Lab{
    public static void main(String[] args) {
        IntSupplier s = () -> (int)(Math.random() * 100) + 1;
        IntConsumer c = i -> System.out.print(i + ", ");
        IntPredicate p = i -> i % 2 == 0;
        IntUnaryOperator op = i -> i / 10 * 10;

        int[] arr = new int[10];

        makeRandomList(s, arr);
        System.out.println(Arrays.toString(arr)); // [84, 11, 21, 51, 58, 83, 77, 75, 15, 58]
        printEventNum(p, c, arr); // [84, 58, 58, ]
        int[] newArr = doSomething(op, arr);
        System.out.println(Arrays.toString(newArr)); // [80, 10, 20, 50, 50, 80, 70, 70, 10, 50]
    }

    static void makeRandomList(IntSupplier s, int[] arr){
        for(int i = 0; i < 10; i++){
            arr[i] = s.getAsInt(); // get()이 아니라 getAsInt()임에 주의
        }
    }
    static int[] doSomething(IntUnaryOperator op, int[] arr){
        int[] newArr = new int[arr.length];

        for(int i = 0; i < newArr.length; i++){
            newArr[i] = op.applyAsInt(arr[i]); // apply()가 아님에 주의
        }

        return newArr;
    }

    static <T> void printEventNum(IntPredicate p, IntConsumer c, int[] arr){
        System.out.print("[");

        for(int i : arr){
            if(p.test(i))
                c.accept(i);
        }
        
        System.out.println("]");
    }
}

— Predicate의 결합

여러 Predicate을 and(), or(), negate()로 연결해서 하나의 새로운 Predicate로 결합할 수 있다.

Predicate<Integer> p = i -> i < 100;
Predicate<Integer> q = i -> i < 200;
Predicate<Integer> r = i -> i % 2 == 0;
Predicate<Integer> notP = p.negate(); // i >= 100

// 100 <= i && (i < 200 || i % 2 == 0)
Predicate<Integer> all = notP.and(q.or(r));
System.out.println(all.test(150)); // true

static 메서드인 isEqual()은 두 대상을 비교하는 predicate을 만들 때 사용

먼저 isEqual()의 매개변수로 비교대상을 하나 지정하고 또 다른 비교대상은 test()의 매개변수로 지정한다.

boolean result = Predicate.isEqual(str1).test(str2);

6) 메서드 참조

람다식이 하나의 메서드만 호출하는 경우에는 메서드참조라는 방법으로 람다식을 간략히 할 수 있다.

Function<String, Integer> f = (String s) -> Integer.parseInt(s);

// 메서드 참조
Function<String, Integer> f = Integer::parseInt;
BiFunction<String, String, Boolean> f = (s1, s2) -> s1.equals(s2);

// 메서드 참조
BiFunction<String, String, Boolean> f = String::equals;

참조변수 f의 타입만 봐도 람다식이 두 개의 String타입 매개변수를 받는다는 것을 알 수 있으므로 람다식의 매개변수 생략 가능

두 개의 String을 받아서 Boolean을 반환하는 equals라는 이름의 메서드는 다른 클래스에도 존재할 수 있기 때문에 equals 앞에 클래스 이름은 반드시 필요

Function<String, Boolean> f = (x) -> obj.equals(x); // 람다식
Function<String, Boolean> f2 = obj::equals; // 메서드 참조
종류람다메서드 참조
static 메서드 참조(x) -> ClassName.method(x)ClassName::method
인스턴스 메서드 참조(obj, x) -> obj.method(x)ClassName::method
특정 객체 인스턴스 메서드 참조(x) -> obj.method(x)obj::method

— 생성자의 메서드 참조

생성자를 호출하는 람다식도 메서드 참조로 변환 가능

Supplier<MyClass> s = () -> new MyClass();
Supplier<MyClass> s = MyClass::new;

// 매개변수가 있는 생성자
Function<Integer, MyClass> f = (i) -> new MyClass(i); // 람다식
Function<Integer, Myclass> f2 = MyClass::new;

BiFunction<Integer, String, MyClass> bf = (i, s) -> new MyClass(i, s);
BiFunction<Integer, String, MyClass> bf2 = MyClass::new;

— 배열 생성

Function<Integer, int[]> f = x -> new int[x]; // 람다식
Function<Integer, int[]> f2 = int[]:: new; // 메서드 참조

메서드 참조는 람다식을 마치 static 변수처럼 다룰 수 있게 해준다. 메서드 참조는 코드를 간략하게 하는데 유용해서 많이 사용되므로, 람다식을 메서드 참조로 변환하는 연습을 많이 해야 함

2. 스트림

for, iterator 등의 방식은 길고 알아보기 어렵다
Collection, Iterator 인터페이스로 컬렉션을 다루는 방식을 표준화하기는 했지만 각 컬렉션 클래스에는 같은 기능의 메서드들이 중복 정의. ex) Collections.sort(), Arrays.sort()

⇒ 해결방안: Stream

데이터소스를 추상화. 데이터를 다루는데 자주 사용되는 메서드 정의.

import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

class Lab{
    public static void main(String[] args) {
        String[] strArr = {"aaa", "bbb", "ccc"};
        List<String> stringList = Arrays.asList(strArr);

        Stream<String> strStream1 = stringList.stream();
        Stream<String> strStream2 = Arrays.stream(strArr);

        strStream1.sorted().forEach(System.out::println);
        strStream2.sorted().forEach(System.out::println);
    }
}

1) 특징: 데이터 소스, 일회용, 내부반복

* 스트림은 데이터 소스를 변경하지 않는다
필요하다면 정렬된 결과를 컬렉션이나 배열에 담아서 반환할 수도 있다.

List<String> sortedList = strStream.sorted().collect(Collectors.toList());

* 스트림은 일회용이다.
한 번 사용하면 닫혀서 다시 사용할 수 없다. 필요하다면 재생성.

strStream1.sorted().forEach(System.out::println);
int numOfStr = strStream1.count(); // error! 스트림 이미 닫힘

* 스트림은 작업을 내부 반복으로 처리한다.
내부반복: 반복문을 메서드의 내부에 숨긴다.
forEach() — 매개변수에 대입된 람다식을 데이터 소스의 모든 요소에 적용, 메서드 안으로 for문을 넣은 것

2) 스트림의 연산

중간연산: 연산 결과가 스트림인 연산, 스트림에 연속해서 중간 연산할 수 있음
최종연산: 연산 결과가 스트림이 아닌 연산, 스트림의 요소를 소모하므로 단 한번만 가능

stream.distinct().limit(5).sorted().forEach(System.out::println);

— Stream에 정의된 연산

메소드설명
Stream distinct()중복을 제거
Stream filter(Predicate predicate)조건에 안 맞는 요소 제외
Stream limit(long maxSize)스트림의 일부를 잘라낸다
Stream skip(long n)스트림의 일부를 건너뛴다
Stream peek(Consumer action)스트림의 요소에 작업수행
Stream sorted()
Stream sorted(Comparator comparator)
스트림의 요소를 정렬한다
Stream map(Function<T, R> mapper)
DoubleStream mapToDouble(ToDoubleFunction mapper)
IntStream mapToInt(ToIntFunction mapper)
LongStream mapToLong(ToLongFunction mapper)
Stream flatMap(Function<T, Stream> mapper)
DoubleStream flatMapToDouble(Function<T, DoubleStream> m)
IntStream flatMapToInt(Function<T, IntStream> m)
LongStream flatMapToLong(Function<T, LongStream> m)스트림의 요소를 변환한다
void forEach(Consumer<? super T> action)
void forEachOrdered(Comsumer<? super T> action)각 요소에 지정된 작업 수행
long count()스트림의 요소의 개수 반환
Optional max(Comparator<? super T> comparator)
Optional min(Comparator<? super T> comparator)
스트림의 최대값 최소값을 반환
Optional findAny()아무거나 하나
Optional findFirst()첫번째 요소 스트림의 요소 하나를 반환
boolean allMatch(Predicate p)모두 만족하는지
boolean anyMatch(Predicate p)하나라도 만족하는지
boolean noneMatch(Predicate<> p)모두 만족하지 않는지
주어진 조건을 모든 요소가 만족시키는지, 만족시키지 않는지
Object[] toArray()
A[] toArray(IntFunction<A[]> generator)
스트림의 모든 요소를 배열로 반환
Optional reduce(BinaryOperator accumulator)
T reduce(T identity, BinaryOperator accumulator)
U reduce(U identity, BiFunction<U, T, U> accumulator, BInaryOperator combiner)
스트림의 요소를 하나씩 줄여가며 계산한다
R collect(Collector<T, A, R> collector)
R collect(Supplier supplier, BiConsumer<R, T> accumulator, BiConsumer<R, R> combiner)
스트림의 요소를 수집한다. 주로 요소를 그룹화하거나 분할한 결과를 컬렉션에 담아 반환하는데 사용한다.

중간 연산은 map(), flapMap(), 최종연산은 reduce(), collect()가 핵심

5) 지연된 연산

스트림 연산에서 중요한 점은 최종 연산이 수행되기 전까지는 중간 연산이 수행되지 않는다는 것.

6) Stream와 IntStream

오토박싱/언박싱으로 인한 비효율을 줄이기 위해 데이터소스의 요소를 기본형으로 다루는 스트림, IntStream, LongStream, DoubleStream이 제공된다.

7) 병렬 스트림

스트림으로 데이터를 다룰 때의 장점
내부적으로 fork&join 프레임워크를 이용하여 자동적으로 연산을 병렬로 수행
parallel()
(병렬로 처리되지 않게 하려면 sequential())
모든 스트림은 기본적으로 병렬 스트림이 아니므로 sequential()을 호출할 필요가 없고, parallel()을 취소할 때만 사용

int sum = strStream.parallel()
					.mapToInt(s -> s.length())
					.sum();

3. 스트림 만들기

1) 컬렉션

Stream<T> Collection.stream()

List<Integer> list = Arrays.aList(1, 2, 3, 4, 5); // 가변인자
Stream<Integer> intStream = list.stream(); // list를 소스로 하는 컬렉션 생성

intStream.forEach(System.out::println); // list의 모든 요소를 출력한다
intStream.forEach(System.out::println); // 에러. 스트림이 이미 닫혔다.

2) 배열

배열을 소스로 스트림을 생성하는 메서드는 Stream과 Arrays에 static method로 정의되어 있다.

Stream<T> Stream.of(T... values) // 가변인자
Stream<T> Stream.of(T[])
Stream<T> Arrays.stream(T[])
Stream<T> Arrays.stream(T[] array, int startInclusive, int endExclusive)

문자열 스트림은 다음과 같이 생성한다.

Stream<String> strStream = Stream.of("a", "b", "c"); // 가변인자
Stream<String> strStream = Stream.of(new String[] {"a", "b", "c"});
Stream<String> strStream = Arrays.stream(new String[] {"a", "b", "c"});
Stream<String> strStream = Arrays.stream(new String[] {"a", "b", "c"}, 0, 3);

int, long, double과 같은 기본형 배열을 소스로 하는 스트림을 생성하는 메서드도 있다.

IntStream IntStream.of(int... values)
IntStream IntStream.of(int[])
IntStream Arrays.stream(int[])
IntStream Arrays.stream(int[] array, int startInclusive, int endExclusive)

3) 특정 범위의 정수

range(), rangeClosed()

IntStream.range(int begin, int end) // end가 범위에 포함되지 않음
IntStream.rangeClosed(int begin, int end) // 포함됨

더 큰 범위의 스트림을 생성하려면 LongStream에 있는 동일한 이름의 메서드를 사용하면 됨

4) 임의의 수

IntStream intStream = new Random().ints(); // 무한 스트림
intStream.limit(5).forEach(System.out::println); // 5개 요소만 출력한다.

아래의 메서드는 매개변수로 스트림의 크기를 지정해서 유한스트림을 반환하므로 limit()을 사용하지 않아도 된다.

IntStream ints(long streamSize)
LongStream longs(long streamSize)
DoubleStream doubles(long streamSize)
지정된 범위의 난수를 발생시키는 스트림을 얻는 메서드

IntStream ints(int begins, int end)
LongStream longs(long begins, long end)
DoubleStream doubles(double begin, double end)

IntStream ints(long streamSize, int begin, int end)
LongStream longs(long streamSize, long begin, long end)
DoubleStream doubles(double streamSize, double begin, double end)

5) 람다식 — iterate(), generate()

람다식을 매개변수로 받아서 이 람다식에 의해 계산되는 값들을 요소로 하는 무한 스트림을 생성한다.

Stream<Integer> evenStream = Stream.iterate(0, n -> n+2); // 0, 2, 4, 6

generate()도 iterate()처럼 람다식에 의해 계산되는 값을 요소로 하는 무한 스트림을 생성해서 반환하지만, iterate()과 달리 이전 결과를 이용해서 다음 요소를 계산하지 않는다.

Stream<Double> randomStream = Stream.generate(Math::random);
Stream<Integer> oneStream = Stream.generate (() -> 1);

generate()에 정의된 매개변수의 타입은 Supplier이므로 매개변수가 없는 람다식만 허용됨.

주의사항: iterate()과 generate()에 의해 생성된 스트림은 기본형 스트림타입의 참조변수로 다룰 수 없다는 것이다.

6) 파일

list(): 지정된 디렉토리에 있는 파일의 목록을 소스로하는 스트림을 생성해서 반환

Stream<String> Files.lines(Path path)
Stream<String> Files.lines(Path path, Charset cs)
Stream<String> lines() // BufferedReader 클래스의 메서드

7) 빈 스트림

Stream emptyStream = Stream.empty(); // 빈 스트림을 생성해서 반환
long count = emptyStream.count(); // count값은 0

8) 두 스트림의 연결

Stream의 static 메서드인 concat()을 사용하면 두 스트림을 하나로 연결할 수 있다.

연결하려는 두 스트림의 요소는 같은 타입이어야 한다.

String[] str1 = {"123", "456", "789"};
String[] str2 = {"abc", "def", "ghi"};

Stream<String> strs1 = Stream.of(str1);
Stream<String> strs2 = Stream.of(str2);
Stream<String> strs3 = Stream.concat(strs1, strs2);

strs3.forEach(System.out::print);
// 123456789abcdefghi

0개의 댓글