프로그래밍 노트/JAVA

[JAVA] Consumer 함수적 인터페이스(Functional Interface)

깡냉쓰 2019. 7. 1. 23:16
728x90
반응형

Consumer 함수적 인터페이스는 리턴값이 없는 accept() 메소드를 가지고 있다.

Consumer는 단지 매개값을 소비하는 역할만 하며, 소비한다는 말은 사용만하고 리턴값이 없다는 뜻이다.


인터페이스명 추상 메소드 설명
Consumer<T> void accept(T t) 객체를 T를 받아 소비
BiConsumer<T,U> void accept(T t, U u) 객체 T, U를 받아 소비
DoubleConsumer void accept(double value) double 값을 받아 소비
intConsumer void accept(int value) int 값을 받아 소비
LongConsumer void accept(long value) long 값을 받아 소비
ObjDoubleConsumer<T> void accept(T t, double value) 객체 T와 double 값을 받아 소비
ObjIntConsumer<T> void accept(T t, int value) 객체 T와 int 값을 받아 소비
ObjLongConsumer<T> void accept(T t, long value) 객체 T와 long 값을 받아 소비
public static void main(String[] args) {
        Consumer<String> consumer = s -> System.out.println(s + " World!");
        consumer.accept("Hello");

        BiConsumer<String, String> biConsumer = (t, u) -> System.out.println(t + " " + u); // 두개의 파라미터를 소비
        biConsumer.accept("Hello", "World!");

        DoubleConsumer doubleConsumer = d -> System.out.println("num : " + d);
        doubleConsumer.accept(10);

        ObjIntConsumer<String> objIntConsumer = (t, u) -> System.out.println(t + u); // 객체, int 파라미터 소비
        objIntConsumer.accept("num : ", 10);
}

2019/06/30 - [프로그래밍 노트/JAVA] - [JAVA] java.util.function FunctionalInterface(함수적 인터페이스) 종류

728x90
반응형