Method Reference
μλμ κ°μ΄ μ¬μ©νλ©° ν΄λΉ ν΄λμ€μ (μΈμ€ν΄μ€) λ©μλλ₯Ό ::
μΌλ‘ λΆμ¬μ μ¬μ©νλ€.
Class :: (Instance) Method
Method μ Reference λ₯Ό μ λ¬νλλ€λ μλ―Έμ΄κΈ° λλ¬Έμ Lamda λ³΄λ€ μΆμ½λ λ¬Έλ²μ΄λ€.
Java μ Method λ μΌκΈ κ°μ²΄κ° μλκΈ° λλ¬Έμ κ°μ²΄μ Reference λ₯Ό μ λ¬νλ λ°©μμΌλ‘ λμλλ€.
Function<String, Integer> func = str -> Integer.parseInt(str);
Function
μΈν°νμ΄μ€λ νλμ μΈμλ₯Ό λ°μ λ€λ₯Ένμ
μ λ¦¬ν΄ νμ
μ κ°λ apply()
Method λ₯Ό κ°κ³ μλ ν¨μν μΈν°νμ΄μ€μ΄λ€.
μ μ½λλ₯Ό μλμ κ°μ΄ Method Reference ννλ‘ λ°κΏμ μ¬μ© κ°λ₯νλ€.
Function<String, Integer> func = Integer::parseInt;
Integer result = f.apply("123");
λν μλμ κ°μ΄ μ μΈμ μ½λ©μ ν΅νμ¬ κ°λ΅ν ν μ μλ€.
Function<String, Boolean> func = String::isEmpty;
Boolean result = func.apply("123");
isEmpty()
λ static method λ μλμ§λ§ μΈμλ‘ λμ΄μ€λ νμ
μ΄ String
μ΄κΈ° λλ¬Έμ ν΄λΉ νμ
μ λ©μλλ₯Ό νΈμΆν΄μ€λ€.
λ νΌλ°μ€λ³ νμ μΆλ‘ (Type Inference) λ°©λ²
- Static Method Reference
Function<String, Integer> func = Integer::parseInt;
Integer result = func.apply("123");
- Instance Method Reference
Function<String, Boolean> func = String::isEmpty;
Boolean result = func.apply("123");
- Constructor Method Reference
μμ±μλ λ©μλ λ νΌλ°μ€λ‘ μμ± κ°λ₯νλ€.
Supplier
λ μΈμ μμ΄ return
λ§ νλ λ©μλλ₯Ό κ°μ§κ³ μμΌλ©° λ©μλ νΈμΆμ new
λ‘ μμ±λ String
κ°μ²΄λ₯Ό 리ν΄νλ€.
μ΄λ λ¬Όλ‘ κΈ°λ³Έ μμ±μλ₯Ό νΈμΆνκ² λλ€.
Supplier<String> str = String::new;
- λ°κΉ₯ μΈμ€ν΄μ€ λ©μλ λ νΌλ°μ€
String str = "Hello";
Predicate<String> pre = str::equals;
pre.test("world");
Comparator<String> compare = String::compareTo;
μμ£Ό μ¬μ©νλ ν¨μν μΈν°νμ΄μ€
Functional Interface | Parameter Type | Return Type | Abstract Method | Description | Other Method |
---|---|---|---|---|---|
Runnable | μμ | void | run | μΈμλ λ°ν κ° μμ΄ μ‘μ μ μννλ€. | μμ |
Supplier | μμ | T | get | T | νμ κ°μ 곡κΈνλ€. |
Consumer | T | void | accept | T | νμ κ°μ μλΉνλ€. |
BiConsumer<T, U> | T, U | void | accept | Tμ Uνμ κ°μ μλΉνλ€. | andThen |
Function<T, R> | T, R | apply | T | νμ μΈμλ₯Ό λ°λ ν¨μλ€. | compose andThen identity |
BiFunction<T, U, R> | T, U, R | apply | Tμ Uνμ μΈμλ₯Ό λ°λ ν¨μλ€. | andThen | |
UnaryOperator | T | T | apply | T νμ μ μ μ©νλ λ¨ν μ°μ°μλ€. | compose andThen identity |
BinaryOperator | T, T | T | apply | T νμ μ μ μ©νλ μ΄ν μ°μ°μλ€. | andThen maxBy minBy |
Predicate | T | boolean | test | Boolean κ°μ λ°ννλ ν¨μλ€. | and or negate isEqual |
BiPredicate<T, U> | T, U | boolean | test | λ κ°μ§ μΈμλ₯Ό λ°κ³ boolean κ°μ λ°ννλ ν¨μλ€. | and or negate |
β Functional Interface Stream β