[JAVA] 42. 람다 표현식 - 유형

문정준's avatar
Feb 17, 2025
[JAVA] 42. 람다 표현식 - 유형
람다 표현식 (Lambda Expression)
  1. 소비 (값 사용 : 인수 O, return X)
  1. 공급 (값 공급(생성) : 인수 X, return O)
  1. 예측(논리) (값 예측(판별) : 인수 O, return boolean)
  1. 함수 (함수형 : 인수 O, return O)
  1. 호출 (기본형 : 인수 X, return X)
package ex07.ch02; // 람다 표현법 // 1. Consumer // 2. Supplier // 3. Predicate : T / F // 4. Function // 5. Callable // 소비자 (사용만) : 인수로 받고, 소비 (리턴 없음) interface Con01 { void accept(int n); } // 공급자 : 값 제공 (인수 없음) interface Sup01 { int get(); } // 논리 (예측) : T / F (boolean) 반환 interface Pre01 { boolean test(int n); } // 함수 : 인수, 리턴 값 모두 존재 interface Fun01 { int apply(int n1, int n2); } // 호출 : 함수 호출 (제일 기본, 인수 & 리턴 X) interface Call01 { void call(); } // 사용법만 익히고, 직접 만들어 쓰지 말 것!! public class Beh02 { public static void main(String[] args) { // 1. Consumer Con01 c1 = (n) -> { System.out.println("소비함 : " + n); }; c1.accept(10); // 2. Supplier Sup01 s1 = () -> 1; // 중괄호 생략 : 자동으로 return 코드 생성 ( 1줄일 때만 가능 ) int r1 = s1.get(); System.out.println("공급받음 : " + r1); // 3. Predicate Pre01 p1 = (n) -> n % 2 == 0; // 중괄호 생략 Pre01 p2 = (n) -> n % 3 == 0; // 중괄호 생략 System.out.println("예측함 : " + p1.test(5)); System.out.println("예측함 : " + p2.test(6)); // 4. Function Fun01 add = (n1, n2) -> n1 + n2; Fun01 sub = (n1, n2) -> n1 - n2; Fun01 mul = (n1, n2) -> n1 * n2; Fun01 div = (n1, n2) -> n1 / n2; System.out.println("함수(더하기) : " + add.apply(1, 2)); System.out.println("함수(빼기) : " + sub.apply(1, 2)); System.out.println("함수(곱하기) : " + mul.apply(1, 2)); System.out.println("함수(나누기) : " + div.apply(1, 2)); // 5. Callable Call01 cl1 = () -> System.out.println("호출되었습니다."); cl1.call(); } }
 
notion image
Share article

sxias