관리 메뉴

Tech Log πŸ› οΈ

[java] λžŒλ‹€μ‹ λ³Έλ¬Έ

TIL

[java] λžŒλ‹€μ‹

sehaan 2023. 2. 25. 23:17

ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ°μ΄λž€,

ν•¨μˆ˜λ₯Ό μ •μ˜ν•˜κ³  데이터 μ²˜λ¦¬λΆ€λ‘œ 보내 데이터λ₯Ό μ²˜λ¦¬ν•˜λŠ” 기법

ν•¨μˆ˜ν˜• ν”„λ‘œκ·Έλž˜λ°μ„ μœ„ν•΄ μžλ°”8λΆ€ν„° λžŒλ‹€μ‹μ„ μ§€μ›ν•œλ‹€.

λžŒλ‹€μ‹ :(λ§€κ°œλ³€μˆ˜, ...) -> {μ²˜λ¦¬λ‚΄μš©}

 

μžλ°”λŠ” λžŒλ‹€μ‹μ„ 읡λͺ… κ΅¬ν˜„ 객체둜 λ³€ν™˜ν•œλ‹€.

 

좔상 λ©”μ†Œλ“œκ°€ ν•˜λ‚˜μ΄κ³  λ‘κ°œμ˜ λ§€κ°œλ³€μˆ˜λ₯Ό κ°€μ§€λŠ” λžŒλ‹€μ‹μ€ λ‹€μŒκ³Ό κ°™λ‹€.

(x,y) -> { μ²˜λ¦¬λ‚΄μš© };

x,y : λ§€κ°œλ³€μˆ˜

-> { μ²˜λ¦¬λ‚΄μš© } : 좔상 λ©”μ†Œλ“œμ˜ μ‹€ν–‰ 블둝

 

λžŒλ‹€μ‹μ€ μΈν„°νŽ˜μ΄μŠ€μ˜ 읡λͺ… κ΅¬ν˜„ κ°μ²΄μ΄λ―€λ‘œ μΈν„°νŽ˜μ΄μŠ€ νƒ€μž…μ˜ λ§€κ°œλ³€μˆ˜μ— λŒ€μž…λ  수 μžˆλ‹€.

public class Main {
    public static void main(String[] args) {
        action((x, y) -> {
            int result = x+y;
            System.out.println("result: "+ result);
        });

        action((x,y) -> {
            int result = x-y;
            System.out.println("result ="+result);
        });
    }

    public static void action(Calculable calculable) {
        int x = 10;
        int y = 4;
        calculable.calculate(x, y);
    }
}

calculable.calculate(x,y) κ°€ μ‹€ν–‰λ˜λ©΄ λžŒλ‹€μ‹μ˜ μ€‘κ΄„ν˜Έ 블둝이 μ‹€ν–‰λœλ‹€.

 

μ–΄λ–€ λžŒλ‹€μ‹μ„ λ§€κ°œκ°’μœΌλ‘œ μ œκ³΅ν•˜λŠλƒμ— 따라 κ²°κ³ΌλŠ” 달라진닀.

 

* μΈν„°νŽ˜μ΄μŠ€μ˜ 읡λͺ… κ΅¬ν˜„ 객체λ₯Ό λžŒλ‹€μ‹μœΌλ‘œ ν‘œν˜„ν•˜λ €λ©΄ μΈν„°νŽ˜μ΄μŠ€κ°€ 단 ν•˜λ‚˜μ˜ μΆ”μƒλ©”μ†Œλ“œλ₯Ό κ°€μ Έμ•Όν•œλ‹€.

( @FuntionalInterface λ₯Ό λΆ™μ΄λŠ” 것은 본인 λ§ˆμŒμ΄μ§€λ§Œ μ •ν™•ν•œ ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€λ₯Ό μž‘μ„±ν•  수 μžˆλ‹€. )

λ§€κ°œλ³€μˆ˜κ°€ μ—†λŠ” λžŒλ‹€μ‹

//싀행문이 λ‘κ°œ 이상일 경우
() ->{
	μ‹€ν–‰λ¬Έ;
        μ‹€ν–‰λ¬Έ;
}

//싀행문이 ν•˜λ‚˜μΌ 경우
() -> μ‹€ν–‰λ¬Έ

싀행문이 두 개 이상일 κ²½μš°μ—λŠ” μ€‘κ΄„ν˜Έλ₯Ό μƒλž΅ν•  수 μ—†λ‹€.

public static void main(String[] args) {
        Person person = new Person();

        person.action(() -> {
            System.out.println("WORK");
            System.out.println("PROGRAMMING");
        });

        person.action(()->{
            System.out.println("WORK");
        });
    }

 

λ§€κ°œλ³€μˆ˜κ°€ μžˆλŠ” λžŒλ‹€μ‹

ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€μ™€ 좔상 λ©”μ†Œλ“œμ— λ§€κ°œλ³€μˆ˜κ°€ μžˆμ„ 경우

 

λ§€κ°œλ³€μˆ˜νƒ€μž…μ€ μƒλž΅ν•˜κ±°λ‚˜ varλ₯Ό μ‚¬μš©ν• μˆ˜λ„ μžˆμ§€λ§Œ νƒ€μž…μ„ μƒλž΅ν•˜λŠ” 것이 μΌλ°˜μ μ΄λ‹€.

 

리턴값이 μžˆλŠ” λžŒλ‹€μ‹

ν•¨μˆ˜ν˜• μΈν„°νŽ˜μ΄μŠ€μ˜ 좔상 λ©”μ†Œλ“œμ— 리턴값이 μžˆμ„ 경우return λ¬Έ ν•˜λ‚˜λ§Œ μžˆμ„ κ²½μš°μ—λŠ” μ€‘κ΄„ν˜Έμ™€ ν•¨κ»˜ return ν‚€μ›Œλ“œ μƒλž΅κ°€λŠ₯리턴값은 연산식 or  리턴값이 μžˆλŠ” λ©”μ†Œλ“œ 호좜둜 λŒ€μ²΄ κ°€λŠ₯

 

public static void main(String[] args) {
        Person person = new Person();

        person.action((x, y) -> {
            double result = x+y;
            return result;
        });

        person.action((x,y) -> (x+y));

    }

 

λ©”μ†Œλ“œ μ°Έμ‘°

public class Computer {
    public static double staticMethod(double x,double y){
        return x+y;
    }public double instanceMethod(double x,double y){
        return x*y;
    }
}
public static void main(String[] args) {
        Person person = new Person();

        person.action(Computer::staticMethod);

        Computer com = new Computer();

        person.action(com::instanceMethod);

    }

 

λ§€κ°œλ³€μˆ˜μ˜ λ©”μ†Œλ“œ μ°Έμ‘°

 

λžŒλ‹€μ‹μ—μ„œ μ œκ³΅λ˜λŠ” a λ§€κ°œλ³€μˆ˜μ˜ λ©”μ†Œλ“œλ₯Ό ν˜ΈμΆœν•΄μ„œ b λ§€κ°œλ³€μˆ˜λ₯Ό λ§€κ°œκ°’μœΌλ‘œ μ‚¬μš©ν•˜λŠ” κ²½μš°κ°€ μžˆλ‹€.

(a,b) -> {a.instanceMethod(b);}

이것을 λ©”μ†Œλ“œ 참쑰둜 ν‘œν˜„ν•˜λ©΄ λ‹€μŒκ³Ό κ°™λ‹€.

클래슀::instanceMethod

a의 클래슀 :: λ©”μ†Œλ“œ 이름

 

public class MethodReference {
    public static void main(String[] args) {
        Person person = new Person();
        person.ordering(String :: compareToIgnoreCase);
    }
}

 

μƒμ„±μž μ°Έμ‘°

λžŒλ‹€μ‹μ΄ μ•„λž˜μ™€ 같이 λ‹¨μˆœνžˆ 객체λ₯Ό μƒμ„±ν•˜κ³  λ¦¬ν„΄ν•˜λ„λ‘ κ΅¬μ„±λœλ‹€λ©΄ μƒμ„±μž 참쑰둜 λŒ€μΉ˜ν•  수 μžˆλ‹€.

(a,b) -> {return new 클래슀(a,b);}

이것은 μƒμ„±μž 참쑰둜 ν‘œν˜„ν•˜λ©΄ λ‹€μŒκ³Ό κ°™λ‹€.

클래슀 :: new