[TIL] λλ€
ππ» λλ€ Lambda
( λ§€κ°λ³μ ) -> { μ€νμ½λ }
- μ΅λͺ ν¨μλ₯Ό μμ±νκΈ° μν μμΌλ‘ ν¨μ μ§ν₯ μΈμ΄μ κ°κΉμ
- μνμμ μ¬μ©νλ ν¨μλ₯Ό λ³΄λ€ λ¨μνκ² νννλ λ°©λ²
- λ§€κ°λ³μλ₯Ό κ°μ§ μ½λ λΈλ‘μ΄μ§λ§ λ°νμμ μ΅λͺ ꡬν κ°μ²΄ μμ±
π‘ μλ°μμ λλ€μμ μ¬μ©νλ μ΄μ ?
: λλ€μμ μ¬μ©νλ©΄ μλ° μ½λκ° λ§€μ° κ°κ²°ν΄μ§κ³
컬λ μ μ μμλ₯Ό νν°λ§νκ±°λ λ§€νν΄μ μνλ κ²°κ³Ό μ§κ³ μ¬μ
πλλ€μ κΈ°λ³Έ λ¬Έλ²
1οΈβ£ κΈ°λ³Έμμ±
(νμ
λ§€κ°λ³μ) -> { ... }
- λ§€κ°λ³μλ₯Ό μ΄μ©νμ¬ μ€κ΄νΈ μ€νλΈλ‘μ μ€ννλ€λ μλ―Έ
2οΈβ£ λ§€κ°λ³μκ° 1κ° μΌ λ ( ) μλ΅ κ°λ₯
λ§€κ°λ³μ -> { ... }
- λ§€κ°λ³μκ° μλ€λ©΄ κ΄νΈ μλ΅νλ©΄ μ λ¨
- νλμ μ€νλ¬Έλ§ μλ€λ©΄ μ€κ΄νΈλ μλ΅ κ°λ₯
3οΈβ£ λ§€κ°λ³μκ° 2κ° μ΄μμ΄κ³ 리ν΄λ¬Έλ§ μ‘΄μ¬νλ©΄ return μλ΅ κ°λ₯
(λ§€κ°λ³μ 1, λ§€κ°λ³μ 2) -> 리ν΄κ° ;
4οΈβ£ λ§€κ°λ³μκ° 2κ° μ΄μμ΄κ³ μ€νλ¬Έμ μ€ννκ³ κ²°κ³Όκ° λ¦¬ν΄
(λ§€κ°λ³μ 1, λ§€κ°λ³μ 2) -> { ... };
βπ» ν¨μν μΈν°νμ΄μ€
: λ¨ νλμ μΆμ λ©μλλ§μ ν¬ν¨νλ μΈν°νμ΄μ€
- μλ°μμ λ©μλ μ¬μ© ? ν΄λμ€ κ°μ²΄ μμ± ν μμ±ν κ°μ²΄μμ λ©μλ νΈμΆ
- 곡ν΅μ μΈ κΈ°λ₯ μ¬μ© ? ν΄λμ€λ§λ€ λ©μλ μ μ
ππ» μ΄λ₯Ό ν΄κ²°νκΈ° μν΄ μΈν°νμ΄μ€ λ¬Έλ²μ νμ©ν 'λλ€μ' λ±μ₯
π νκ² νμ κ³Ό ν¨μν μΈν°νμ΄μ€
- νλμ μΆμ λ©μλκ° μ μΈλ μΈν°νμ΄μ€(=ν¨μν μΈν°νμ΄μ€)λ§ λλ€μμ νκ² νμ μ΄ λ μ μμ
- @FunctionalInterface : μ»΄νμΌλ¬κ° μΆμ λ©μλκ° λ κ° μ΄μμ΄ λμ§ μλλ‘ μ²΄ν¬ν΄μ£Όλ μ λν μ΄μ
βͺοΈ λ§€κ°λ³μμ λ¦¬ν΄ κ°μ΄ μλ λλ€μ
@FunctionalInterface
public interface LambdaTest {
public void check();
}
package Fri;
import static Fri.LambdaTest.*;
public class LambdaTestExample {
public static void main(String[] args) throws Exception {
LambdaTest test;
test = () -> {
String str = "First";
System.out.println(str);
};
test.check();
test = () -> {
System.out.println("Second");
};
test.check();
}
}
- test.check( )λ‘ νΈμΆν΄μ£Όμ΄μΌ λλ€μμ μ€κ΄νΈ μ€νμν΄
βͺοΈ λ§€κ°λ³μκ° μλ λλ€μ
package Fri;
public interface LambdaTest2 {
public void okay(int i);
}
package Fri;
public class LambdaEx {
public static void main(String[] args) {
LambdaTest2 test2;
test2 = (i) -> {
int num = i * 10;
System.out.println(num);
};
test2.okay(3);
test2 = (i) -> System.out.println(i*10);
test2.okay(5);
}
}
βͺοΈ λ¦¬ν΄ κ°μ΄ μλ λλ€μ
package Fri;
public interface LambdaTest3 {
public int calcu(int a, int b);
}
package Fri;
public class LambdaEx3 {
public static void main(String[] args) {
LambdaTest3 test3;
test3 = (a, b) -> {
int output = a + b;
return output;
};
int output = test3.calcu(4, 5);
System.out.println(output);
test3 = (a, b) -> { return a+b; };
int output2 = test3.calcu(2, 5);
System.out.println(output2);
test3 = (a, b) -> a+b;
int output3 = test3.calcu(3,5);
System.out.println(output3);
test3 = (a, b) -> sum(a,b);
int output4= test3.calcu(1, 3);
System.out.println(output4);
}
public static int sum(int a , int b){
return a + b;
}
}
βπ» λ©μλ λ νΌλ°μ€
- λλ€μμ λ κ°λ¨νκ² νννλ λ°©λ²
- λλ€μμμ λΆνμν λ§€κ°λ³μ μ κ±°
- λλ€μμ λ©μλλ₯Ό μ¬μ©νλΌκ³ κ°λ₯΄ν€λ λ°©μμ΄λΌλ©΄ λ©μλ λ νΌλ°μ€λ μ§μ μ°Έμ‘°νλ λ°©μ (κ°λ μ± up)
(1) μ μ λ©μλ μ°Έμ‘°
ν΄λμ€ :: λ©μλ ;
(2) μΈμ€ν΄μ€ λ©μλ μ°Έμ‘°
μ°Έμ‘°λ³μ :: λ©μλ ;
- μΌλ°μ μΌλ‘ ν΄λμ€λͺ κ³Ό λ©μλλͺ μ¬μ΄μ :: λ₯Ό λ£μ΄ μ¬μ©
(Apple apple) -> apple.madeJam()
Apple ::madeJam
(str, x) -> str.happy
String::happy
(String str) -> System.out.println(str)
System.out::println
(String str) -> happy.birth(str)
happy::birth
- μΈν°νμ΄μ€μ μΆμ λ©μλμ λ§€κ°λ³μ λ° λ¦¬ν΄ νμ μ λ°λΌ λ¬λΌμ§
- μΈμ€ν΄μ€ λ©μλλ λ¨Όμ κ°μ²΄λ₯Ό μμ±ν ν μ°Έμ‘°λ³μ :: λ©μλ
π λ§€κ°λ³μμ λ©μλ λ νΌλ°μ€
- λ©μλλ λλ€μ μΈλΆμ ν΄λμ€ λ©€λ²μΌ μ μκ³ λλ€μ λ΄λΆμ λ§€κ°λ³μμ λ©€λ² μ€ νλ
π μμ±μ μ°Έμ‘°
- μμ±μλ₯Ό μ°Έμ‘°νλ€λ κ²μ κ°μ²΄ μμ±μ μλ―Έ
- μμ±μκ° μ€λ²λ‘λ©λμ΄ μ¬λ¬ κ° μμ κ²½μ° λμΌν λ§€κ°λ³μ νμ κ³Ό κ°μλ₯Ό κ°λ μμ±μλ₯Ό μ°Ύμ μ€ν
(a, b) -> { return new ν΄λμ€ (a, b); };
ν΄λμ€ :: new
[ λλ€μ ]
Wash<Fruit> Wash1 = () -> new Fruit();
Fruit fruit1 = Wash1.get();
[ λ©μλ λ νΌλ°μ€ ]
Wash<Fruit> Wash2 = Fruit::new;
Fruit fruit2 = Wash2.get();