2025-03-21
μ€λμ μλ°μμ μμ£Ό μ¬μ©λλ ν¨μν νλ‘κ·Έλλ°κ³Ό μ΄λ
Έν
μ΄μ
(Annotation)μ μ§μ€μ μΌλ‘ νμ΅νλ€.
μ΄ λ μ£Όμ λ μλ°μ μ μΈν νλ‘κ·Έλλ°κ³Ό λ©ν νλ‘κ·Έλλ°μ κ°λ₯νκ² νλ ν΅μ¬ λ¬Έλ²μ΄λ€.
μ΄ ν¬μ€νΈμμλ κ° κ°λ
μ μ μλΆν° μ€μ μ½λ μμ , μμ© λ° μ€ν κ²°κ³ΌκΉμ§ μ 리νλ€.
λλ€μ
, ν¨μν μΈν°νμ΄μ€
, μ€νΈλ¦Ό API
λ₯Ό νμ©νμ¬ κ΅¬ν@FunctionalInterface
interface Func1 {
void say(String message); // μΆμ λ©μλ 1κ°λ§ μ‘΄μ¬ν΄μΌ ν¨
}
@FunctionalInterface
interface Func2 {
int sum(Integer... args); // κ°λ³ μΈμ ν©μ°
}
@FunctionalInterface
interface Func3 {
List<Integer> createListDesc(int... args); // μ μ λ°°μ΄ β λ΄λ¦Όμ°¨μ List λ°ν
}
public class C01FunctionalInterfaceMain {
public static void main(String[] args) {
// λ¬Έμμ΄ μΆλ ₯ ν¨μ
Func1 func1 = (msg) -> System.out.println("λ©μμ§: " + msg);
func1.say("Hello!");
// μ«μ ν©μ° ν¨μ
Func2 func2 = (nums) -> Arrays.stream(nums).reduce(0, Integer::sum);
System.out.println("ν©κ³: " + func2.sum(10, 20, 30));
// μ μ λ°°μ΄μ λ΄λ¦Όμ°¨μμΌλ‘ μ λ ¬ ν Listλ‘ λ³ν
Func3 func3 = (nums) -> Arrays.stream(nums)
.boxed()
.sorted((a, b) -> b - a)
.collect(Collectors.toList());
System.out.println("λ΄λ¦Όμ°¨μ 리μ€νΈ: " + func3.createListDesc(5, 3, 9, 1));
}
}
Function<Integer, Integer> square = x -> x * x;
Function<Integer, Integer> doubleIt = x -> x * 2;
// square ν doubleIt μν: (x * x) * 2
Function<Integer, Integer> squareThenDouble = square.andThen(doubleIt);
System.out.println(squareThenDouble.apply(5)); // 25 β 50
π andThen() μμ½
f.andThen(g)
λ μνμ μΌλ‘ g(f(x))
λ₯Ό μλ―Ένλ€.
// ν¨μλ₯Ό 리ν΄νλ ν¨μ
Function<Integer, Function<Integer, Integer>> curriedAdd = x -> y -> x + y;
System.out.println(curriedAdd.apply(10).apply(20)); // 30
// 3λ¨κ³ 컀λ§
Function<Integer, Function<Integer, Function<Integer, Integer>>> func3args =
x -> y -> z -> x + y + z;
System.out.println(func3args.apply(1).apply(2).apply(3)); // 6
// List<Object> μ€ Integerλ§ κ³¨λΌ ν©μ°
Function<List<Object>, Integer> sumOnlyIntegers = list ->
list.stream()
.filter(el -> el instanceof Integer)
.map(el -> (Integer) el)
.reduce(0, Integer::sum);
@FunctionalInterface
interface Functional {
Integer execute(int... args);
}
class Calc {
Functional sum, sub, mul, div;
Calc() {
sum = (args) -> Arrays.stream(args).reduce(0, Integer::sum);
sub = (args) -> Arrays.stream(args)
.boxed()
.sorted((a, b) -> b - a)
.reduce((a, b) -> a - b)
.orElse(0);
mul = (args) -> Arrays.stream(args).reduce(1, (a, b) -> a * b);
div = (args) -> Arrays.stream(args)
.boxed()
.sorted((a, b) -> b - a)
.mapToInt(Integer::intValue)
.reduce((a, b) -> a / b)
.orElse(0);
}
}
// μ€ν μμ
Calc calc = new Calc();
System.out.println("λ§μ
: " + calc.sum.execute(10, 20, 30)); // 60
System.out.println("λΊμ
: " + calc.sub.execute(100, 20, 10)); // 70
System.out.println("κ³±μ
: " + calc.mul.execute(2, 3, 4)); // 24
System.out.println("λλμ
: " + calc.div.execute(100, 2, 5)); // 10
@
κΈ°νΈλ‘ μμνλ©° ν΄λμ€, λ©μλ, νλ λ±μ λΆμΌ μ μμμ΄λ Έν μ΄μ | μ€λͺ |
---|---|
@Override | μ€λ²λΌμ΄λ© νμΈ |
@Deprecated | λ μ΄μ μ¬μ©λμ§ μμ |
@FunctionalInterface | ν¨μν μΈν°νμ΄μ€ |
@SuppressWarnings | κ²½κ³ μ΅μ |
@SafeVarargs | κ°λ³ μΈμ μ λ€λ¦ κ²½κ³ μ΅μ |
μ΄λ Έν μ΄μ | μ€λͺ |
---|---|
@Retention | μ μ§ μ μ± (SOURCE, CLASS, RUNTIME) |
@Target | μ μ© μμΉ (CLASS, METHOD λ±) |
@Documented | javadoc ν¬ν¨ μ¬λΆ |
@Inherited | μμ μ¬λΆ |
@Repeatable | λ°λ³΅ μ μ© κ°λ₯ |
@Retention(RetentionPolicy.RUNTIME) // λ°νμ μ μ§
@Target({ElementType.TYPE, ElementType.METHOD}) // ν΄λμ€/λ©μλμ μ μ© κ°λ₯
public @interface CustomAnnotation {
String value() default "";
int number() default 0;
boolean isOpen() default true;
}
@CustomAnnotation(value = "HELLO WORLD", number = 5, isOpen = true)
class Simple {
String v1;
int v2;
Simple() {
CustomAnnotation ref = this.getClass().getAnnotation(CustomAnnotation.class);
System.out.println("value: " + ref.value());
System.out.println("number: " + ref.number());
System.out.println("isOpen: " + ref.isOpen());
this.v1 = ref.value();
this.v2 = ref.number();
}
@Override
public String toString() {
return "Simple[v1=" + v1 + ", v2=" + v2 + "]";
}
}
public class Main {
public static void main(String[] args) {
Simple s = new Simple();
System.out.println(s);
}
}
value: HELLO WORLD
number: 5
isOpen: true
Simple[v1=HELLO WORLD, v2=5]
μ£Όμ | λ°°μ΄ λ΄μ© |
---|---|
ν¨μν νλ‘κ·Έλλ° | λλ€μ, ν¨μν μΈν°νμ΄μ€, μ€νΈλ¦Ό, κ³ μ°¨ ν¨μ, μ»€λ§ |
ν¨μ μ°κ²° | andThen() μΌλ‘ ν¨μ 체μ΄λ |
Object νν°λ§ | filter + map + reduce λ‘ νμ
λ³ μ²λ¦¬ |
μ΄λ Έν μ΄μ | κΈ°λ³Έ + λ©ν + μ¬μ©μ μ μ μ΄λ Έν μ΄μ |
리νλ μ | getAnnotation() μΌλ‘ λ°νμμ μ 보 μΆμΆ |
μ€λμ μλ°μ λ ν΅μ¬ μ£Όμ μΈ ν¨μν νλ‘κ·Έλλ°κ³Ό μ΄λ
Έν
μ΄μ
μ μ€μ΅νλ©° κ°λ
μ νμ€ν μ΅νλ€.
νΉν, λλ€μκ³Ό andThen()
μ ν΅ν΄ ν¨μλ₯Ό 쑰립νλ― μ¬μ©ν μ μλ€λ κ²μ΄ λ§€μ° ν₯λ―Έλ‘μ λ€.
λν, μ΄λ
Έν
μ΄μ
μ μ§μ μ μνκ³ λ¦¬νλ μ
μΌλ‘ κ°μ μ½λ κ³Όμ μ
μ€μ νλ μμν¬(Spring λ±)κ° μ΄λ»κ² μλνλμ§λ₯Ό μ΄ν΄νλ λ° ν° λμμ΄ λμλ€.
μ΄μ μ€λ¬΄μμλ ν¨μν μ¬κ³ μ μ΄λ Έν μ΄μ κΈ°λ° μ€μ μ μμ μκ² νμ©ν μ μμ κ² κ°λ€! πͺ