JAVA · 람다/메서드
Predicate / Consumer / Supplier / Function
Java 표준 함수형 인터페이스 4가지의 시그니처와 활용법입니다.
람다/메서드중급SupplierConsumerPredicateFunction
핵심 설명
Java 표준 함수형 인터페이스 4가지의 시그니처와 활용법입니다.
Java code
import java.util.*;
import java.util.function.*;
public class StdFunctional {
public static void main(String[] args) {
// Supplier<T> — 인자 없이 T 반환
Supplier<List<String>> listFactory = ArrayList::new;
List<String> list = listFactory.get();
// Consumer<T> — T를 받고 반환 없음
Consumer<String> printer = System.out::println;
printer.accept("Hello Consumer");
// Predicate<T> — T를 받고 boolean 반환
Predicate<Integer> isPositive = n -> n > 0;
System.out.println(isPositive.test(5)); // true
// Function<T, R> — T를 받고 R 반환
Function<String, Integer> toLength = String::length;
System.out.println(toLength.apply("Java")); // 4
// 실전 활용: 필터 + 변환 + 소비
List<String> names = List.of("Alice", "Bob", "Charlie", "Dave");
Predicate<String> longName = s -> s.length() > 3;
Function<String, String> toUpper = String::toUpperCase;
names.stream()
.filter(longName)
.map(toUpper)
.forEach(printer);
}
}학습 팁
표준 함수형 인터페이스를 사용하면 람다 호환성이 보장됩니다. 커스텀 인터페이스보다 표준 인터페이스를 우선하세요.
주의할 점
Function<Integer, Integer>보다 UnaryOperator<Integer>가 적합합니다. 입출력 타입이 같으면 특화 인터페이스를 사용하세요.
자주 묻는 질문
Predicate / Consumer / Supplier / Function란 무엇인가요?
Java 표준 함수형 인터페이스 4가지의 시그니처와 활용법입니다.
Predicate / Consumer / Supplier / Function 학습 시 주의할 점은 무엇인가요?
Function<Integer, Integer> 보다 UnaryOperator<Integer> 가 적합합니다. 입출력 타입이 같으면 특화 인터페이스를 사용하세요.
Continue Learning
Java 학습을 이어가세요
총 200개의 독립 HTML 학습 문서 중 하나입니다. 각 문서는 고유 URL과 canonical 메타데이터를 가집니다.