
1. Supplier
Supplier <T>
(T는 Parameter Type을, R은 Return Type을 의미한다)
Supplier는 매개변수는 없고, 반환값만 존재하는 경우 사용된다.
    @Test
    @DisplayName("Supplier를 활용한 양수 확인")
    void supplier() {
        Predicate<Integer> isPositive = n -> n > 0;
        Supplier<Integer> getValue = () -> (int) (Math.random() * 100) + 1;
        assertThat(isPositive.test(getValue.get())).isTrue();
    }
2. Consumer
Consumer <T>
Consumer는 매개변수만 있고, 반환값이 없는 경우 사용된다.
    @Test
    @DisplayName("Consumer를 활용한 출력")
    void consumer() {
        Consumer<String> printStrUpper = str -> System.out.println(str.toUpperCase());
        printStrUpper.accept("consumer");
    }
    @Test
    @DisplayName("Consumer의 andThen을 활용한 method chaining 출력")
    void consumerAndThen() {
        List<String> citrus = Arrays.asList("orange", "mandarin", "tangerine");
        Consumer<List<String>> printList = list -> {
            for(String fruit : list) {
                System.out.println(fruit);
            }
        };
        Consumer<List<String>> printListUpper = list -> {
            for(String fruit : list) {
                System.out.println(fruit.toUpperCase());
            }
        };
        printList.andThen(printListUpper).accept(citrus);
    }
3. Function
Function<T, R>
Fuction은 일반적인 함수로, 하나의 매개변수를 받아서 결과를 반환할때 사용된다.
    @Test
    @DisplayName("Function을 활용한 양수 확인")
    void function() {
        Function<Integer, Integer> getFirstDigit = n -> n % 10;
        assertThat(getFirstDigit.apply(123)).isEqualTo(3);
    }

https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html
java.util.function (Java Platform SE 8 )
Interface Summary Interface Description BiConsumer Represents an operation that accepts two input arguments and returns no result. BiFunction Represents a function that accepts two arguments and produces a result. BinaryOperator Represents an operation u
docs.oracle.com
'개발' 카테고리의 다른 글
| [Spring] java.lang.IllegalArgumentException: id to load is required for loading 오류 (0) | 2022.09.18 | 
|---|---|
| [Java] UnaryOperator, BinaryOperator 함수형 인터페이스 (0) | 2022.06.27 | 
| [Java] Predicate 함수형 인터페이스 (0) | 2022.06.27 | 
| IntelliJ에서 JUnit, AssertJ 프로젝트 생성하기 (0) | 2022.06.27 | 
| [Java] HashMap 내부 코드에 존재하는 transient 키워드 (0) | 2022.06.23 | 
 
									
								 
									
								