๐ช ๋๋ค์์ JAVA 8 ๋ฒ์ ์ ์๋ก ์ถ๊ฐ๋ ๊ธฐ๋ฅ์ผ๋ก ๋ฉ์๋๋ฅผ ํ๋์ ์(expression)์ผ๋ก ํํํ ๊ฒ์ด๋ค.
๋ฉ์๋๋ฅผ ๋๋ค์์ผ๋ก ํํํ๋ฉด ๋ฉ์๋ ์ด๋ฆ๊ณผ ๋ฆฌํด๊ฐ์ด ์์ด์ ์ต๋ช ํจ์๋ผ๊ณ ๋ ํ ์ ์๋ค.
f(x, y) = x * y
(x, y) -> x * y
โ=โ ์ด โ ๋ก ๋ฐ๋๊ฒ ๋๊ณ f ๊ฐ ์ฌ๋ผ์ง ๊ฒ์ ๋ณผ ์ ์๋ค. ์ฆ, ํจ์ ์ด๋ฆ์ ๋นผ๊ณ โ ์ดํ๋ก ์์ด ๋ฐ๋ก ์ค๊ฒ ๋๋ค.
โ๋ฉ์๋์ ์ด๋ฆ๊ณผ ๋ฐํ๊ฐ์ด ์์ด์ง๋คโ ๋ ์๋ฏธ๋ ์ต๋ช ํจ์๋ฅผ ์๋ฏธํ๋ค.
๋๋ค์์ ๊ฐ์ฅ ํฐ ์ฅ์ ์ ๋จ์ํจ ๊ทธ๋ฆฌ๊ณ ํธํจ์ด๋ค.
์ต์ํ์ง ์์ ํํ์๊ณผ ์ฌ์ฉ๋ฒ์ ์ฒ์์๋ ์ด๋ ค์ธ ์ ์์ผ๋,
๋๋ค์์ ํ์ฉํ ์ ์๊ฒ๋๋ฉด ์ปฌ๋ ์ , ์คํธ๋ฆผ์ ์ฐ๊ณํด์ ๋ฐ์ดํฐ๋ฅผ ์ฝ๊ฒ ์กฐ์ํ ์ ์๊ฒ ๋๋ค.
๋ํ ๋ถํ์ํ๊ฒ ๋ฐ๋ณต๋๋ ์ฝ๋๋ ์ ๊ฑฐํ ์ ์๊ฒ ๋๋ค.
๋ฌ๋์ปค๋ธ๊ฐ ์กด์ฌํ๊ณ ํธ๋ถํธ๊ฐ ๋ง์ด ๊ฐ๋ฆฌ๊ณ ์์ผ๋ ๋ฐฐ์ด ์ดํ ์ฌ์ฉํ๊ฒ ๋๋ค๋ฉด, ๋๋ค์์ผ๋ก ๋ฐ๊พธ๊ณ ์ถ์ ์๊ฐ์ด ๋ถ์ฅ๋ถ์ฅ ๋๊ฒ ๋ ๊ฒ์ด๋ค.
// ๋งค๊ฐ ๋ณ์ ์๋ ๊ฒฝ์ฐ
() -> { ... }
// ๋งค๊ฐ ๋ณ์ ์๋ ๊ฒฝ์ฐ
(ํ์
๋งค๊ฐ๋ณ์, ...) -> { ... }
๋๋ค ํํ์์์ ๋งค๊ฐ ๋ณ์ ํ์ ์ ๋ฐํ์ ์์ ์๋์ผ๋ก ์ธ์๋๊ธฐ ๋๋ฌธ์ ๋งค๊ฐ๋ณ์ ํ์ ์ ์ผ๋ฐ์ ์ผ๋ก ์ธ๊ธํ์ง ์์๋ ๋๋ค.
ํ๋์ ๋งค๊ฐ๋ณ์๋ง ์กด์ฌํ๋ ๊ฒฝ์ฐ ( ) ๋ ์๋ตํ ์ ์๋ค. ์คํ๋ฌธ์ด ํ๋์ธ ๊ฒฝ์ฐ์ { } ๋ ์๋ต ๊ฐ๋ฅํ๋ค.
์๋ฐ์์๋ ๋ฉ์๋๋ฅผ ๋ฐ๋ก ์ ์ธํ ์ ์๋ค. ๋ฉ์๋๋ ํญ์ ํด๋์ค์ ๋ด๋ถ์์๋ง ์ ์ธ๋๊ธฐ ๋๋ฌธ์ ๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์๋ ๊ฐ์ฒด๋ฅผ ์์ฑํด์ผ ํ๋ค.
๋ฐ๋ผ์ ๋๋ค์์ ๋จ๋ ์ผ๋ก ์ ์ธํ ์ ์๋ค. ๋๋ค์์ ์ต๋ช ํด๋์ค์ฒ๋ผ ์ธํฐํ์ด์ค ๋ณ์์ ๋์ ๋๋ค.
์ธํฐํ์ด์ค๋ ์ง์ ๊ฐ์ฒดํ ํ ์ ์๊ธฐ ๋๋ฌธ์ ๊ตฌํ ํด๋์ค๊ฐ ํ์ํ๋ฐ ๋๋ค์์ ํตํด ์ต๋ช ๊ตฌํ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ฌ ์ฌ์ฉํ ์ ์๋ค.
๋๋ค์์ด ๋์ ๋ ์ธํฐํ์ด์ค๋ฅผ ๋๋ค์์ ํ๊น ํ์ ์ด๋ผ๊ณ ํ๋ค.
ํ์ง๋ง ๋ชจ๋ ์ธํฐํ์ด์ค๊ฐ ๋๋ค์์ ํ๊น ํ์ ์ด ๋ ์ ์๋ค.
๋๋ค์์ ํ๋์ ๋ฉ์๋๋ฅผ ์ ์ํ๊ธฐ ๋๋ฌธ์ ๋ ๊ฐ ์ด์์ ์ถ์ ๋ฉ์๋๊ฐ ์ ์ธ๋ ์ธํฐํ์ด์ค๋ ๋๋ค์์ ์ด์ฉํด ๊ฐ์ฒด๋ฅผ ์์ฑํ ์ ์๋ค.
์ฆ, ํ๋์ ์ถ์ ๋ฉ์๋๊ฐ ์ ์ธ๋ ์ธํฐํ์ด์ค๋ง ๋๋ค์์ ํ๊น ํ์ ์ด ๋ ์ ์๋ค.
์ด๋ฌํ ์ธํฐํ์ด์ค๋ฅผ ํจ์์ ์ธํฐํ์ด์ค(functional interface)๋ผ๊ณ ํ๋ค.
์ปดํ์ผ๋ฌ๊ฐ ๋ ๊ฐ ์ด์์ ์ถ์ ๋ฉ์๋๊ฐ ์ ์ธ๋์๋์ง ์ฒดํฌํด์ฃผ๋ ์ด๋ ธํ ์ด์ ์ด ์๋ค.
@FunctionalInterface
์ด๋
ธํ
์ด์
์ด๋ค. ์ด ์ด๋
ธํ
์ด์
์ ์ต์
์ด๋ค. ์์ด๋ ์ถ์ ๋ฉ์๋ ํ๋๋ง ์์ผ๋ฉด ํจ์์ ์ธํฐํ์ด์ค๋ก ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
@FunctionalInterface
public interface MyFunction {
public void methodA();
public void methodB(); // Error
}
์๋ฐ 8์์๋ ๋น๋ฒํ๊ฒ ์ฌ์ฉ๋๋ ํจ์์ ์ธํฐํ์ด์ค๋ฅผ java.util.function
ํ์ค API ํจํค์ง๋ก ์ ๊ณตํ๋ค.
ํฌ๊ฒ Consumer
, Supplier
, Function
, Operator
, Predicate
๋ก ๊ตฌ๋ถ๋๋ค.
Consumer ํจ์์ ์ธํฐํ์ด์ค์ ํน์ง์ ๋ฆฌํด ๊ฐ์ด ์๋ accept()
๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์๋ค๋ ๊ฒ์ด๋ค.
accept()
๋ฉ์๋๋ ๋จ์ง ๋งค๊ฐ ๋ณ์๋ก ๋์ด์จ ๊ฐ์ ์๋นํ๋ ์ญํ ๋ง ํ๋ค. ์๋นํ๋ค๋ ๊ฒ์ ๋ฆฌํด๊ฐ์ด ์๋ค๋ ๋ง์ด๋ค.
์ธํฐํ์ด์ค ๋ช | ์ถ์ ๋ฉ์๋ | ์ค๋ช |
---|---|---|
Consumer | void accept(T t) | ๊ฐ์ฒด T๋ฅผ ๋ฐ์์ ์๋นํ๋ค. |
BiConsumer<T, U> | void accept(T t, U u) | ๊ฐ์ฒด T, U๋ฅผ ๋ฐ์ ์๋นํ๋ค. |
intConsumer | void accept(int value) | int ๊ฐ์ ๋ฐ์ ์๋นํ๋ค. |
DoubleConsumer | void accept(double value) | double ๊ฐ์ ๋ฐ์ ์๋นํ๋ค. |
LongConsumer | void accept(long value) | long ๊ฐ์ ๋ฐ์ ์๋นํ๋ค. |
ObjIntConsumer | void accept(T t, int value) | ๊ฐ์ฒด T์ int ๊ฐ์ ๋ฐ์ ์๋นํ๋ค. |
ObjDoubleConsumer | void accept(T t, double value) | ๊ฐ์ฒด T์ double ๊ฐ์ ๋ฐ์ ์๋นํ๋ค. |
ObjLongConsumer | void accept(T t, long value) | ๊ฐ์ฒด T์ long ๊ฐ์ ๋ฐ์ ์๋นํ๋ค. |
Consumer<String> consumer = str -> System.out.println(str + "์ด(๊ฐ) ์
๋ ฅ๋จ");
consumer.accept("Hello?");
BiConsumer<String , LocalTime> biConsumer =
(str,time) -> System.out.println(str + "์ด(๊ฐ) " + time + "์ ์
๋ ฅ๋จ");
biConsumer.accept("Hello?", LocalTime.now());
ObjIntConsumer<Random> objectIntConsumer =
(random,bound) -> System.out.println("0๋ถํฐ" + bound + "์ ๊น์ง์ ๋์ ๋ฐ์" + random.nextInt(bound));
objectIntConsumer.accept(new Random(),10);
}
Hello?์ด(๊ฐ) ์
๋ ฅ๋จ
Hello?์ด(๊ฐ) 18:09:23.022936800์ ์
๋ ฅ๋จ
0๋ถํฐ10์ ๊น์ง์ ๋์ ๋ฐ์1
Supplier ํจ์์ ์ธํฐํ์ด์ค๋ ๋งค๊ฐ๋ณ์๊ฐ ์๊ณ ๋ฆฌํด ๊ฐ์ด ์๋ getXXX()
๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
์ด ๋ฉ์๋๋ ์คํ ๋๋ฉด ํธ์ถํ ๊ณณ์ผ๋ก ๊ฐ์ ๋ฆฌํดํด์ค๋ค.
์ธํฐํ์ด์ค ๋ช | ์ถ์ ๋ฉ์๋ | ์ค๋ช |
---|---|---|
Supplier | T get() | T ๊ฐ์ฒด๋ฅผ ๋ฆฌํดํ๋ค. |
BooleanSupplier | Boolean getAsBoolean() | Boolean ๊ฐ์ ๋ฆฌํดํ๋ค. |
IntSupplier | int getAsInt() | int ๊ฐ์ ๋ฆฌํดํ๋ค. |
DoubleSupplier | double getAsDouble() | double ๊ฐ์ ๋ฆฌํดํ๋ค. |
LongSuplier | long getAsLong() | long ๊ฐ์ ๋ฆฌํดํ๋ค. |
public static void main(String[] args) {
Supplier<LocalDateTime> supplier = () -> LocalDateTime.now();
System.out.println(supplier.get());
BooleanSupplier booleanSupplier = () -> {
int random = (int)(Math.random() * 2);
return random == 0? false : true;
};
System.out.println("๋๋ค true or false: " +booleanSupplier.getAsBoolean());
}
2024-01-26T18:14:42.105426900
๋๋ค true or false: false
applyXXX()
๋ฅผ ๊ฐ์ง๊ณ ์๋ค๋ ๊ฒ์ด๋ค. ์ธํฐํ์ด์ค ๋ช | ์ถ์ ๋ฉ์๋ | ์ค๋ช |
---|---|---|
Function <T, R> | R apply(T t) | ๊ฐ์ฒด T๋ฅผ ๊ฐ์ฒด R๋ก ๋งคํํ๋ค. |
BiFunction <T, U, R> | R apply(T t, U u) | ๊ฐ์ฒด T์ U๋ฅผ ๊ฐ์ฒด R๋ก ๋งคํํ๋ค. |
IntFunction | R apply(int value) | int๋ฅผ ๊ฐ์ฒด R๋ก ๋งคํํ๋ค. |
IntToDoubleFunction | double applyAsDouble(int value) | int๋ฅผ double๋ก ๋งคํํ๋ค. |
IntToLongFunction | long applyAsLong(int value) | int๋ฅผ long์ผ๋ก ๋งคํํ๋ค. |
DoubleFunction | R apply(double value) | double์ ๊ฐ์ฒด R๋ก ๋งคํํ๋ค. |
LongToDoubleFunction | double applyAsDouble(long value) | long์ double๋ก ๋งคํํ๋ค. |
LongToIntFunction | int applyAsInt(long value) | long์ int๋ก ๋งคํํ๋ค. |
ToDoubleBiFunction<T, U> | double applyAsDouble(T t, U u) | ๊ฐ์ฒด T์ U๋ฅผ double๋ก ๋งคํํ๋ค. |
ToDoubleFunction | double applyAsDouble(T value) | ๊ฐ์ฒด T๋ฅผ double๋ก ๋งคํํ๋ค. |
ToIntBiFunction<T, U> | int applyAsInt(T t, U u) | ๊ฐ์ฒด T์ U๋ฅผ int๋ก ๋งคํํ๋ค. |
ToIntFunction | int applyAsInt(T t) | ๊ฐ์ฒด T๋ฅผ int๋ก ๋งคํํ๋ค. |
ToLongBiFunction<T, U> | long applyAsLong(T t, U u) | ๊ฐ์ฒด T์ U๋ฅผ long์ผ๋ก ๋งคํํ๋ค. |
ToLongFunction | long applyAsLong(T t) | ๊ฐ์ฒด T๋ฅผ long์ผ๋ก ๋งคํํ๋ค. |
public static void main(String[] args) {
Function<String, Integer> function = str -> Integer.valueOf(str);
String strValue = "12345";
System.out.println(function.apply(strValue));
BiFunction<String,String ,Integer> biFunction =
(str1,str2) -> Integer.valueOf(str1) + Integer.valueOf(str2);
System.out.println(biFunction.apply("12345","11111")
}
12345
23456
Operator ํจ์์ ์ธํฐํ์ด์ค๋ Function ๊ณผ ๋๊ฐ์ด ์๋ํ๋ค. ๋งค๊ฐ๋ณ์์ ๋ฆฌํด๊ฐ์ด ์๋ applyXXX()
๋ฉ์๋๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
Function ๊ณผ ๋ค๋ฅธ ์ ์ ๋งค๊ฐ๋ณ์์ ๋์ผํ ํ์ ์ผ๋ก ๋ฆฌํด์ ํ๋ค.
์ธํฐํ์ด์ค ๋ช | ์ถ์ ๋ฉ์๋ | ์ค๋ช |
---|---|---|
BinaryOperator | BiFunction<T, U, R>์ ํ์ ์ธํฐํ์ด์ค | T์ U๋ฅผ ์ฐ์ฐํ์ฌ R๋ก ๋ฆฌํดํ๋ค. |
UnaryOperator | Function<T, R> ์ ํ์ ์ธํฐํ์ด์ค | T๋ฅผ ์ฐ์ฐํ ํ R๋ก ๋ฆฌํดํ๋ค. |
DoubleBinaryOperator | double applyAsDouble(double, double) | ๋งค๊ฐ๋ณ์ ๋ ๊ฐ๋ฅผ ํ์ฉํ์ฌ double ํ์ ์ผ๋ก ๋ฆฌํดํ๋ค. |
DoubleUnaryOperator | double applyAsDouble(double) | ๋งค๊ฐ๋ณ์ ํ ๊ฐ๋ฅผ ํ์ฉํ์ฌ double ํ์ ์ผ๋ก ๋ฆฌํดํ๋ค. |
IntBinaryOperator | int applyAsInt(int, int) | ๋ ๊ฐ์ int๋ฅผ ์ฐ์ฐํ์ฌ int ํ์ ์ผ๋ก ๋ฆฌํดํ๋ค |
IntUnaryOperator | int applyAsInt(int) | ํ ๊ฐ์ int๋ฅผ ์ฐ์ฐํ์ฌ int ํ์ ์ผ๋ก ๋ฆฌํดํ๋ค. |
LongBinaryOperator | long applyAsLong(long, long) | ๋ ๊ฐ์ long์ ์ฐ์ฐํ์ฌ long ํ์ ์ผ๋ก ๋ฆฌํดํ๋ค. |
LongUnaryOperator | long applyAsLong(long) | ํ ๊ฐ์ long์ ์ฐ์ฐํ์ฌ long ํ์ ์ผ๋ก ๋ฆฌํดํ๋ค. |
public class LamdaEx {
private static int[] intArr = {64, 90, 80, 92, 100};
public static void main(String[] args) {
int max = method((x, y) -> Math.max(x, y));
System.out.println("๊ฐ์ฅ ํฐ ๊ฐ : " + max);
int min = method((x, y) -> Math.min(x, y));
System.out.println("๊ฐ์ฅ ์์ ๊ฐ : " + min);
}
public static int method(IntBinaryOperator intBinaryOperator){
int result = intArr[0];
for (int val : intArr){
result = intBinaryOperator.applyAsInt(result, val);
}
return result;
}
}
๊ฐ์ฅ ํฐ ๊ฐ : 100
๊ฐ์ฅ ์์ ๊ฐ : 64
Predicate ํจ์์ ์ธํฐํ์ด์ค๋ ๋งค๊ฐ ๋ณ์์ boolean ๋ฆฌํด ๊ฐ์ด ์๋ testXXX()
๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
์ด ๋ฉ์๋๋ค์ ๋งค๊ฐ๋ณ์ ๊ฐ์ ์ด์ฉํด true ๋๋ false ๋ฅผ ๋ฆฌํดํ๋ ์ญํ ์ ํ๋ค.
์ธํฐํ์ด์ค ๋ช | ์ถ์ ๋ฉ์๋ | ์ค๋ช |
---|---|---|
Predicate | Boolean test(T t) | ๊ฐ์ฒด T๋ฅผ ์กฐ์ฌํ์ฌ true, false๋ฅผ ๋ฆฌํดํ๋ค. |
BiPredicate<T, U> | Boolean test(T t, U u) | ๊ฐ์ฒด T์ U๋ฅผ ์กฐ์ฌํ์ฌ true, false๋ฅผ ๋ฆฌํดํ๋ค. |
DoublePredicate | Boolean test(double value) | double ๊ฐ์ ์กฐ์ฌํ์ฌ true, false๋ฅผ ๋ฆฌํดํ๋ค. |
IntPredicate | Boolean test(int value) | int ๊ฐ์ ์กฐ์ฌํ์ฌ true, false๋ฅผ ๋ฆฌํดํ๋ค. |
LongPredicate | Boolean test(long value) | long ๊ฐ์ ์กฐ์ฌํ์ฌ true, false๋ฅผ ๋ฆฌํดํ๋ค. |
public static void main(String[] args) {
int[] intArr = {90, 67, 87, 99, 100};
int testCut = 80;
IntPredicate intPredicate = number -> number >= testCut;
for(int num : intArr){
if(intPredicate.test(num)){
System.out.println(num + " ์ " + testCut + " ๋ณด๋ค ๋์ผ๋ฏ๋ก ํฉ๊ฒฉ");
}
else {
System.out.println(num + " ์ " + testCut + " ๋ณด๋ค ๋ฎ์ผ๋ฏ๋ก ๋ถํฉ๊ฒฉ");
}
}
}
90 ์ 80 ๋ณด๋ค ๋์ผ๋ฏ๋ก ํฉ๊ฒฉ
67 ์ 80 ๋ณด๋ค ๋ฎ์ผ๋ฏ๋ก ๋ถํฉ๊ฒฉ
87 ์ 80 ๋ณด๋ค ๋์ผ๋ฏ๋ก ํฉ๊ฒฉ
99 ์ 80 ๋ณด๋ค ๋์ผ๋ฏ๋ก ํฉ๊ฒฉ
100 ์ 80 ๋ณด๋ค ๋์ผ๋ฏ๋ก ํฉ๊ฒฉ
๋ฉ์๋ ์ฐธ์กฐ(method reference)๋ ํจ์ํ ์ธํฐํ์ด์ค๋ฅผ ๋๋ค์์ด ์๋ ์ผ๋ฐ ๋ฉ์๋๋ฅผ ์ฐธ์กฐ์์ผ ์ ์ธํ๋ ๋ฐฉ๋ฒ์ด๋ค.
์ผ๋ฐ ๋ฉ์๋๋ฅผ ์ฐธ์กฐํ๊ธฐ ์ํด์๋ 3๊ฐ์ง ์กฐ๊ฑด์ ๋ง์กฑํด์ผํ๋ค.
ํจ์ํ ์ธํฐํ์ด์ค์ ๋งค๊ฐ๋ณ์ ํ์ /๊ฐฏ์/๋ฐํ ํ์ด๋ ๋ฉ์๋์ ๋งค๊ฐ๋ณ์ ํ์ /๊ฐฏ์/๋ฐํ ํ์ด ๊ฐ์์ผ ํ๋ค.
์์์ ์ธ๊ธํ ์ฐธ์กฐํ ๋ฉ์๋๊ฐ 3๊ฐ์ง ์กฐ๊ฑด์ ๋ง์กฑํ๋์ง ํ์ธํ๋ค.
๋งค๊ฐ๋ณ์๊ฐ ์๊ณ ๋ฐํํ์ด ๋์ผํ ๊ฒฝ์ฐ ๋ฉ์๋ ์ฐธ์กฐ๋ฅผ ์ ์ฉํ ์ ์๋ค.
๋ฉ์๋ ์ฐธ์กฐ ํํ์
ํด๋์ค์ด๋ฆ::๋ฉ์๋์ด๋ฆ (static)์ผ ๊ฒฝ์ฐ
์ฐธ์กฐ๋ณ์์ด๋ฆ::๋ฉ์๋์ด๋ฆ (non-static)์ผ ๊ฒฝ์ฐ
new
๋ก ์์ฑ์ ํ๊ธฐ ๋๋ฌธ์ ํด๋์ค์ด๋ฆ::new ๋ก ์ฐธ์กฐ ํด์ผํ๋ค.๋ฉ์๋ ์ฐธ์กฐ ํํ์
ํด๋์ค์ด๋ฆ::new