[자바 람다표현식] 64. 람다 유형

김건우's avatar
Feb 17, 2025
[자바 람다표현식] 64. 람다 유형

1. Consumer

// 소비하는 친구 interface Con01 { void accept(int n); } public class Beh02 { public static void main(String[] args) { // 1. Consumer Con01 c1 = (n) -> { System.out.println("소비함 : " + n); }; c1.accept(10);

2. Supplier

// 공급하는 친구 interface Sup01 { int get(); } public class Beh02 { public static void main(String[] args) { // 2. Supplier Sup01 s1 = () -> 1; // 중괄호를 안 쓰면 자동으로 리턴코드가 된다. 리턴은 한 줄일때만 생략 가능 int r1 = s1.get(); System.out.println("공급받음 : " + r1);

3. Prsedicate

// 예견하는 친구 interface Pre01 { boolean test(int n); } public class Beh02 { public static void main(String[] args) { // 3. Prsedicate 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

// 함수 interface Fun01 { int apply(int n1, int n2); } public class Beh02 { public static void main(String[] args) { // 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));

5. Callable

// 기본 interface Cal01 { void Call(); } public class Beh02 { public static void main(String[] args) { // 5. Callable Cal01 ca1 = () -> { System.out.println("호출 : "); }; ca1.Call(); }
notion image
Share article

gunwoo