자바 람다

오세훈·2023년 7월 26일
0

java

목록 보기
10/16

lambda

람다

람다식 구현체
매개변수 애로우토큰 함수바디
(x, y) -> {x*y};

() -> { }; : basic style
(x) -> { }; : consumer
() -> {x}; : Supplier
(x) -> {x*x}; :
Operator (매개변수를 전달받아 처리를 한 후 다시 반환하는 타입),
Function (매개변수를 받아 처리를 한 후 그 결과를 형변환하여 반환하는 타입),
Predicate (매개변수를 받아 비교한 후 일치하는지 여부를 논리 값으로 반환하는 타입)

단점

하나의 메소드만 사용 가능

basic style (매개변수x, 리턴값x)

package sec1;

// 람다의 추상체는 기능적 인터페이스
@FunctionalInterface
interface MyLambda1 {
    void print();
}

// 람다식
public class LambdaEx1 {
    /*
        721, 728 페이지
		*/
    public static void main(String[] args) {
        method1();
        MyLambda1 lam1 = () -> { // 선언 및 내용 추가
            System.out.println("람다");
        };

        lam1.print();
    }

    public static void method1() {
        System.out.println("메소드");
    }
}

consumer (매개변수x, 리턴값o)

package sec1;

// consumer
@FunctionalInterface
interface MyLambda2 {
    void print(int x);
}

public class LambdaEx2 {
    public static void main(String[] args) {
        MyLambda1 lam1 = () -> {
            System.out.println("람다2");
        };
        lam1.print();

        MyLambda2 lam2 = (x) -> {
            System.out.println("내 나이는 : "+x);
        };
        lam2.print(26); // 매개변수는 여기에 대입
    }
}

Supplier (매개변수o, 리턴값x)

package sec1;

// Supplier
@FunctionalInterface
interface Lambda3 {
    String print();
}
public class LambdaEx3 {
    public static void main(String[] args) {
        Lambda3 lam3 = () -> {
            return "홍길동";
        };
        String s = lam3.print();
        System.out.println(s);
    }
}

Operator (매개변수o, 리턴값o)

매개변수를 전달받아 처리를 한 후 다시 반환하는 타입

package sec1;

// Operator
@FunctionalInterface
interface MyLambda4 {
    String print(String name);
}

public class LambdaEx4 {
    public static void main(String[] args) {
        MyLambda4 lam4 = name -> {
            return name;
        };
        String name = lam4.print("홍길동");
        System.out.println("내 이름은 : " + name);
    }
}

Function

매개변수를 받아 처리를 한 후 그 결과를 형 변환하여 반환하는 타입

package sec1;

import java.util.Scanner;

// Function
@FunctionalInterface
interface MyLambda5 {
    int print(String a);
}
public class LambdaEx5 {
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) {
        MyLambda5 lam5 = (a) -> {
            return Integer.parseInt(a);
        };
        String s = sc.nextLine();
        int v = lam5.print(s);
        System.out.println(s+"의 제곱 : " + Math.pow(v, 2));
    }
}

Function2

package sec2;

// Function
@FunctionalInterface
interface MyLambda6<T , R> { // 여기에서 T는 매개변수, R는 반환값으로 사용 선언
    R cal(T a, T b);
}

public class LambdaEx6 {

    public static void main(String[] args) {
        // 곱하기
        MyLambda6<Integer, Long> mul = (a, b) -> { // Integer 형 입력, 출력
            return (long) a * b;
        };

        // 나누기
        MyLambda6<Integer, Double> div = (a, b) -> { // Integer 형 입력, Double 형 출력
            return (double) a / b;
        };

        // 더하기
        MyLambda6<Integer, Integer> sum = (a, b) -> {
            return (int) a+b;
        };

        // 빼기
        MyLambda6<Integer, Integer> sub = (a, b) -> {
            return (int) a-b;
        };

        // 나머지
        MyLambda6<Integer, Integer> mod = (a, b) -> {
            return (int) a%b;
        };

        System.out.println(sum.cal(30, 50));
        System.out.println(div.cal(52, 3));
        System.out.println(mul.cal(52, 3));
        System.out.println(sub.cal(52, 3));
        System.out.println(mod.cal(52, 3));
    }
}

Predicate

매개변수를 받아 비교한 후 일치하는지 여부를 논리 값으로 반환하는 타입

package sec2;

// Predicate
@FunctionalInterface
interface MyLambda7<T, Q> {
    boolean compare(T t, Q q);
}

public class LambdaEx7 {
    public static void main(String[] args) {
        MyLambda7<Integer, Integer> comp1 = (a, b) -> {
            return a==b ? true : false;
        };

        MyLambda7<String, String> comp2 = (s, s2) -> {
            return s.equals(s2) ? true : false;
        };

        System.out.println(comp1.compare(10, 20));
        System.out.println(comp2.compare("str", "str"));
        System.out.println(comp1.compare(200, 200));
    }
}
profile
자바 풀 스택 주니어 개발자

0개의 댓글