[Java] 15. Lambda Expression_2.Lambda Expression 유형

김미숙's avatar
Feb 18, 2025
[Java] 15. Lambda Expression_2.Lambda Expression 유형
 
package ex07.ch02; // !! 보고 쓸 수만 있으면 됨 !! /* 1. Consumer 2. Supplier 3. Predicate (true or false) 논리 4. Function 5. Callable */ // 소비하는 친구 -> 인수가 필요함 interface Con01 { void accept(int n); } // 공급하는 친구 interface Sup01 { int get(); // 타입은 달라질 수 있음 } // 예견하는 친구 // 논리 (true or false) interface Pre01 { boolean test(int n); } // 함수 // consumer 와 supplier 를 같이 interface Fun01 { int apply(int n1, int n2); } // 기본 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 // return 타입 필요 Sup01 s1 = () -> 1; // 중괄호 안 쓰면 자동으로 return 코드 됨 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("예측함: " + p1.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 Call01 call = () -> { System.out.println("기본 함수"); }; call.call(); } }
C:\workspace\tools\jdk-21\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\lib\idea_rt.jar=51885:C:\Program Files\JetBrains\IntelliJ IDEA 2024.3.2.1\bin" -Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 -Dsun.stderr.encoding=UTF-8 -classpath C:\workspace\java_lec\study\out\production\study ex07.ch02.Beh02 소비: 10 공급받음: 1 예측함: false 예측함: true 함수: 3 함수: 2 기본 함수 Process finished with exit code 0
Share article

parangdajavous