Java의 정석
14장. 람다식, 메서드참조
simplism
2023. 2. 23. 00:37
람다식(Lambda Expresstion)
람다식 작성하기
람다식 작성시 주의사항
람다식 예
람다식은 익명함수? 익명 객체이다
함수형 인터페이스
익명 객체를 람다식으로 대체
함수형 인터페이스 타입의 매개변수
java.util.function패키지
Predicate의 결합 CF와 함수형 인터페이스
인퍼테이스가 가질수 있는 메서드종류 : default메서드, static 메서드, 추상메서드
public static void main(String[] args) {
Function<String, Integer> f = (s) -> Integer.parseInt(s, 16);
Function<Integer, String> g = (i) -> Integer.toBinaryString(i);
Function<String, String> h = f.andThen(g);
Function<Integer, Integer> h2 = f.compose(g);
System.out.println(h.apply("FF")); // 2진수로변환
System.out.println(h2.apply(2)); // 16진수로 변환
Function<String, String> f2 = x -> x; //항등함수 그대로나옴
System.out.println(f2.apply("AAA"));
Predicate<Integer> p = i -> i < 100;
Predicate<Integer> q = i -> i < 200;
Predicate<Integer> r = i -> i % 2 == 0;
Predicate<Integer> notP = p.negate();
Predicate<Integer> all = notP.and(q.or(r));
System.out.println(all.test(150));
String str1 = "abc";
String str2 = "abc";
Predicate<String> p2 = Predicate.isEqual(str1);
boolean result = p2.test(str2);
System.out.println(result);
}
컬렉션 프레임웍과 함수형 인터페이스
메서드 참조 (method reference)
생성자와 메서드 참조
출처 : 남궁성의 정석코딩