[JAVA] 47. 람다의 유형

편준민's avatar
Feb 18, 2025
[JAVA] 47. 람다의 유형
💡

람다표현식의 종류

  1. consumer (소비)
  1. Supplier (공급)
  1. Predicate (논리 - true or false)
  1. Function (함수)
  1. Callable (기본)
package ex07.ch02; /* 1. consumer 2. Supplier 3. Predicate (true or false) 논리 4. Function 5. Callable */ // 소비하는 친구 (consumer) interface Con01 { void accept(int n); } // 공급하는 친구 (Supplier) interface Sup01 { int get(); } //예견하는 친구 (Predicate) interface Pre01 { boolean test(int n); } // 함수 (Function) interface Fun01 { int apply(int n1, int n2); } // 기본 (Callable) interface Cal01 { 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 =() -> { return 1; } (원래코드) Sup01 s1 = () -> 1; int r1 = s1.get(); System.out.println("공급받음 : " + r1); //3.Predicate Pre01 p1 = (n) -> 5 % 2 == 0; // return식이 1줄이면 중괄호 생략 Pre01 p2 = (n) -> 6 % 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("함수 : " + mul.apply(1, 2)); //5. Callable Cal01 ca1 = () -> { System.out.println("기본 함수"); }; ca1.call(); } }
Share article

YunSeolAn