๐ถ๐ซ๏ธํจ์ํ ํ๋ก๊ทธ๋๋ฐ
Java๋ ๊ฐ์ฒด์งํฅ ์ธ์ด์ด๊ธฐ ๋๋ฌธ์ ๊ธฐ๋ณธ์ ์ผ๋ก ํจ์ํ ํ๋ก๊ทธ๋๋ฐ์ด ๋ถ๊ฐ๋ฅํ๋ค. ํ์ง๋ง JDK8๋ถํฐ Stream API์ ๋๋ค์, ํจ์ํ ์ธํฐํ์ด์ค ๋ฑ์ ์ง์ํ๋ฉด์ Java๋ฅผ ์ด์ฉํด ํจ์ํ์ผ๋ก ํ๋ก๊ทธ๋๋ฐํ ์ ์๋ API ๋ค์ ์ ๊ณตํด์ฃผ๊ณ ์๋ค.
First Class Citizon
- First Class Citizon ์ ์๋์ ์์ฑ๋ค์ ๋ชจ์ฃผ ๋ง์กฑํด์ผ ํฉ๋๋ค.
• ๋ณ์์ ๊ฐ์ ํ ๋นํ ์ ์์ด์ผ ํฉ๋๋ค.
• ํจ์์ ํ๋ผ๋ฏธํฐ๋ก ๋๊ฒจ์ค ์ ์์ด์ผ ํฉ๋๋ค.
• ํจ์์ ๋ฐํ๊ฐ์ด ๋ ์ ์์ด์ผ ํฉ๋๋ค.
https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html
@FunctionalInterface
ํจ์ํ ์ธํฐํ์ด์ค
- ์ถ์ ๋ฉ์๋๊ฐ ๋ฑ ํ๋๋ง ์กด์ฌํ๋ ์ธํฐํ์ด์ค ์ด๋ค.
๐ก public abstract๊ฐ ๋จ ํ๋๋ง ์์ผ๋ฉด ๊ฐ๋ฅํ๋ฉฐ ๋ค๋ฅธ ํํ๋ก ์ ์๊ฐ ๋์ด์ ธ ์๋ ๊ฒ์ ์๊ด์ด ์๋ค.
@FunctionalInterface
public interface PracticeLambda {
void practice(); //public abstract ์๋ต
static void practice2() {
System.out.println("์ฐ์ต1");
}
default void practice3() {
System.out.println("์ฐ์ต2");
}
}
๋ง์ฝ์ ์ถ์ ๋ฉ์๋๊ฐ 2๊ฐ ์ด์์ด๋ฉด @functionalInterface์์ ์ปดํ์ผ ์ค๋ฅ๊ฐ ๋์จ๋ค.
์๋ฐ์์ ์ ๊ณตํ๋ ํจ์ํ ์ธํฐํ์ด์ค
- Function<T,R> , UnaryOperator<T>
- ๋๊ฐ์ ์ธ์๋ฅผ ๋ฐ์์ ํ๋์ ๊ฒฐ๊ณผ๋ฅผ Returnํ๋ค.
- ์ฌ๊ธฐ์ ๋๊ฐ์ ์ธ์์ ํ์ ์ T,R ์๋ก ๋ค๋ฅด๊ฑฐ๋ ๊ฐ์๋ ์๊ด์ด ์๋ค.
- ๋ง์ฝ์ ๋๊ฐ์ ์ธ์๊ฐ ๊ฐ์ ํ์ ์ด๋ฉด UnaryOperator<T>๋ฅผ ์ฌ์ฉ
๐ฆํ์ฌ์ ์ด ์ฝ๋๋ Function ์ธํฐํ์ด์ค๋ฅผ ์ต๋ช ๊ฐ์ฒด ์คํ์ผ๋ก ๊ตฌํ์ ํ๋ค.
public class PracticeLamdba {
public static void main(String[] args) {
Function<Integer, Integer> pricaticeTest = new Function<Integer, Integer>() {
@Override
public Integer apply(Integer integer) {
return integer+10;
}
};
System.out.println(pricaticeTest.apply(3));
}
}
- ๊ฐ์ ๋ฐ์์์ apply๋ฅผ ์ค๋ฒ๋ผ์ด๋ ํด์ +10์ ํด์ฃผ๋ ํจ์๋ฅผ ๋ง๋ค์๋ค.
๐ฆ์ด๋ฌํ ์ต๋ช ์ ๋๋์ ๋๋ค๋ก ํํ์ด ๊ฐ๋ฅํ๋ค.
public class PracticeLamdba {
public static void main(String[] args) {
Function<Integer, Integer> pricaticeTest = integer -> integer+10;
System.out.println(pricaticeTest.apply(3));
}
}
- ์ฝ๋๊ฐ ์์ฒญ ๊ฐ๊ฒฐํ๊ณ ๊ฐ๋ ์ฑ์ด ๋์์ก๋ค.
Function<T,R> → apply()
→T๋ ์ ๋ ฅํ๋ ํ์ ์ ์๋ฏธํ๋ฉฐ R์ return ๋๋ ๊ฐ์ ํ์ ์ ์๋ฏธํ๋ค.
์ฌ๊ธฐ์ ๋ง์ฝ์ Funciotn ํจ์๊ฐ ์ฌ๋ฌ๊ฐ ์์ ๋ Function์ ๋ค์ํ ๊ธฐ๋ฅ์
์ฌ์ฉํ ์ ์๋ค.
- compose
- ๊ฐ๋จํ๊ฒ ์ฝ๋๋ฅผ ๋ณด์ฌ์ฃผ๊ณ ์ค๋ช ์ ํ๊ฒ ๋ค.
public class PracticeLamdba {
public static void main(String[] args) {
Function<Integer, Integer> plus = (number) -> number+10;
Function<Integer , Integer> multiple = number2 ->number2*2;
int result=plus.compose(multiple).apply(10);
System.out.println(result);
}
}
- ๋ํ๊ธฐ ๊ธฐ๋ฅ๊ณผ ๊ณฑํ๊ธฐ ๊ธฐ๋ฅ์ ๋ง๋ค์๊ณ copose๋ฅผ ์ฌ์ฉ์ ํ๋ค.
์ด๋ result์ ๊ฐ์ ์ด๋ป๊ฒ ๋์ฌ๊น? —> ๊ฒฐ๊ณผ๋ 30์ด ๋์ค๊ฒ ๋๋ค.
Compose๋
()์ ๊ธฐ๋ฅ์ ๋จผ์ ์ฐ์ฐ์ ํ๊ณ ๊ทธ ๋ค์ ์ฒซ๋ฒ ์งธ๋ก ์ค๋ ๊ธฐ๋ฅ์ ์ฐ์ฐ์ ํ๋ค.
multiple → ๊ฒฐ๊ณผ → ๊ฒฐ๊ณผ & plus ๋ผ๊ณ ์๊ฐ์ ํ๋ฉด ์ข๋ค.
andThen๋
public class PracticeLamdba {
public static void main(String[] args) {
Function<Integer, Integer> plus = (number) -> number+10;
Function<Integer , Integer> multiple = number2 ->number2*2;
int result=plus.andThen(multiple).apply(10);
System.out.println(result);
}
}
andThen์ compose์ ๋ฐ๋๋ก ๋จผ์ ์ค๋ ๊ธฐ๋ฅ์ ์ฐ์ฐํ๊ณ ๊ทธ ๋ค์ () ๊ธฐ๋ฅ์ ์ฐ์ฐ์ ํ๋ ๊ฒ์ ์๋ฏธํ๋ค.
์ฆ. plus → ๊ฒฐ๊ณผ → multiple์ ํ๋ฉฐ ๊ฒฐ๊ณผ๋ 40์ด ๋์ค๊ฒ ๋๋ค.
- BiFuntion<T,U,R>
- BiFunction์ ์ ๋ ฅ๊ฐ์ด 2๊ฐ๋ฅผ ๋ฐ๊ณ ๋๊ฐ์ ์ฐ์ฐ์ ๋ํ ๊ฒฐ๊ณผ๊ฐ R๋ก
- ๋ฐํํ๋ ๊ธฐ๋ฅ์ ๊ฐ์ง๋ค.
- ๊ธฐ๋ณธ์ ์ธ ๊ธฐ๋ฅ์ Function<T,R>๊ณผ ๋น์ทํ๋ฉฐ ์ ๋ ฅ๊ฐ์ด 2๊ฐ๋ผ๋ ์ฐจ์ด๋ง ์๋ค.
์ต๋ช
public class PracticeLamdba {
public static void main(String[] args) {
BiFunction<Integer,Integer,Integer>biFunction = new BiFunction<Integer, Integer, Integer>() {
@Override
public Integer apply(Integer integer, Integer integer2) {
return integer+integer2;
}
};
int result=biFunction.apply(10 , 20);
System.out.println(result);
}
}
๋๋ค
public class PracticeLamdba {
public static void main(String[] args) {
BiFunction<Integer,Integer,Integer>biFunction = (integer, integer2) -> integer+integer2;
int result=biFunction.apply(10 , 20);
System.out.println(result);
}
}
Function<T,R> & BiFunction<T,U,R> ๊ฐ์ ํ์
<aside> ๐ก ๋ง์ฝ์ T ,U ,R์ด ๋ค ๋๊ฐ์ Integer ํ์ ์ด๋ฉด ๋ค ์ ์ด์ผ ๋๋๊ฐ?
</aside>
๋ง์ฝ์ ํ์ ์ด ๊ฐ์ผ๋ฉด Function์ UnaryOperator<T>๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
๊ทธ๋ฌ๋ฉด ์ ๋ ฅ , ์ถ๋ ฅ๊ฐ์ ํ์ ์ด ๋ชจ๋ T๋ก ์ ํด์ง๋ค.
BiFunction<T,U,R>์ ์ ๋ ฅ ์ถ๋ ฅ ๋ชจ๋ ๊ฐ์ผ๋ฉด BinaryOperator<T>๋ฅผ ์ฌ์ฉ
public class PracticeLamdba {
public static void main(String[] args) {
UnaryOperator<Integer>unaryOperator = (n) -> n+10;
System.out.println(unaryOperator.apply(10));
BinaryOperator<Integer>binaryOperator=(a,b)->a+10+b;
System.out.println(binaryOperator.apply(10,20));
}
}
- Consumer<T> - apply()
Tํ์ ์ ์ ๋ ฅ ๋ฐ์์ ์๋ฌด๊ฐ๋ ๋ฆฌํดํ์ง ์๋ ํจ์ ์ธํฐํ์ด์ค๋ฅผ ์๋ฏธ
์ต๋ช
public class PracticeLamdba {
public static void main(String[] args) {
Consumer<String>consumer = new Consumer<String>() {
@Override
public void accept(String s) {
System.out.println("๋ด ์ด๋ฆ์ "+s +"์
๋๋ค.");
}
};
consumer.accept("๊น๋ฌด๊ฑด");
}
}
๋๋ค
public class PracticeLamdba {
public static void main(String[] args) {
Consumer<String>consumer = (name) -> System.out.println("๋ด ์ด๋ฆ์"+name+"์
๋๋ค.");
consumer.accept("๊น๋ฌด๊ฑด");
}
}
- Suppiler<T>
- T ํ์
์ ๊ฐ์ ์ ๊ณตํ๋ ํจ์ ์ธํฐํ์ด์ค
- ์ธ์๊ฐ ํ์ ์๋ค.
- ๋ด๊ฐ ์ด๋ค ๊ฐ์ ๋ฐ์์ง ๊ฒฐ์
- T get()
- T ํ์
์ ๊ฐ์ ์ ๊ณตํ๋ ํจ์ ์ธํฐํ์ด์ค
์ต๋ช
public class PracticeLamdba {
public static void main(String[] args) {
Supplier<String> supplier = new Supplier<String>() {
@Override
public String get() {
return "๊น๋ฌด๊ฑด";
}
};
System.out.println("๋ด ์ด๋ฆ์ "+supplier.get());
}
}
๋๋ค
public class PracticeLamdba {
public static void main(String[] args) {
Supplier<String> supplier = () -> "๊น๋ฌด๊ฑด";
System.out.println("๋ด ์ด๋ฆ์ "+supplier.get());
}
}
- Predicate<T>
- Tํ์ ์ ๋ฐ์์ Boolean์ ๋ฆฌํดํ๋ ํจ์ ์ธํฐํ์ด์ค
- ํจ์ ์กฐํฉ์ฉ ๋ฉ์๋
- and
- or
- negate()
์ต๋ช
public class PracticeLamdba {
public static void main(String[] args) {
Predicate<Integer>predicate = new Predicate<Integer>() {
@Override
public boolean test(Integer integer) {
return integer%2==0;
}
};
System.out.println(predicate.test(10));
}
}
๋๋ค
public class PracticeLamdba {
public static void main(String[] args) {
Predicate<Integer>predicate = integer -> integer%2==0;
System.out.println(predicate.test(10));
}
}
๋ง์ฝ์ ๋ค์ํ ์กฐ๊ฑด์ด ์๋ค๊ณ ์๊ฐํ๋ฉด and or๋ฅผ ์ด์ฉํ๋ฉด ๋๋ค.
์๋ฅผ ๋ค์ด์ ์ง์์ด๋ฉด์ ๊ฐ์ด 4์ธ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ๊ณ ์ถ๋ค๊ณ ์กฐ๊ฑด์ ๋ง๋ค๋ฉด
- ์ง์
- ๊ฐ์ด 4
์ด ๋๊ฐ์ ์กฐ๊ฑด์ and์ฐ์ฐ์ ํ๋ฉด ๋๋๋ฐ
and
public class PracticeLamdba {
public static void main(String[] args) {
Predicate<Integer>integerPredicate = integer -> integer%2==0;
Predicate<Integer>predicate = num ->num==4;
if(integerPredicate.and(predicate).test(4)){
System.out.println("์ฐธ");
}else System.out.println("๊ฑฐ์ง");
}
}
or
public class PracticeLamdba {
public static void main(String[] args) {
Predicate<Integer>integerPredicate = integer -> integer%2==0;
Predicate<Integer>predicate = num ->num==4;
if(integerPredicate.or(predicate).test(4)){
System.out.println("์ฐธ");
}else System.out.println("๊ฑฐ์ง");
}
}
negate()
public class PracticeLamdba {
public static void main(String[] args) {
Predicate<Integer>integerPredicate = integer -> integer%2==0;
Predicate<Integer>predicate = num ->num==4;
System.out.println(predicate.negate().test(4));
}
}
'๐พBackEnd > ๐ Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
๋๋ค์ ๋ก์ปฌ ๋ณ์ - Effectively Final (0) | 2023.01.28 |
---|