λ©μλ μ°Έμ‘° : κΈ°μ‘΄μ λ©μλ μ μλ₯Ό μ¬νμ©ν΄μ λλ€μ²λΌ μ λ¬ ν μ μκ² ν¨.
-> λλ€ ννμλ³΄λ€ λ κ°λ
μ±μ΄ μ’λ€.
inventory.sort((Apple a1, Apple a2) ->
a1.getWeight().compareTo(a2.getWeight()));
inventory.sort(comparing(Apple::getWeight));
μ μ λ©μλμ°Έμ‘°
Integerμ parselnt λ©μλ => Integer::parselnt
λ€μννμμ μΈμ€ν΄μ€ λ©μλμ°Έμ‘°
Stringμ length λ©μλ => String::length
κΈ°μ‘΄κ°μ²΄μ μΈμ€ν΄μ€ λ©μλμ°Έμ‘°
Transaction κ°μ²΄λ₯Ό ν λΉλ°μ expensiveTransaction μ§μ λ³μκ° μκ³ ,
Transaction κ°μ²΄μλ getValue λ©μλ => expensiveTransaction::getValue
λ§λ‘νλ©΄ μ΄λ €μ°λ μλ κ·Έλ¦Όμ 보μ
λ€μμ λλ€ννμκ³Ό μΌμΉνλ λ©μλμ°Έμ‘°λ₯Ό ꡬννμμ€.
1. ToIntFunction<String> stringToInt =
(String s) -> Integer.parselnt(s);
2. BiPredicate<List<String>, String> contains =
(list, element) -> list.contains(element);
3. Predicate<String> startsWithNumber =
(String string) -> this.startsWithNumber(string);
μ λ΅
1. Function<String, Integer> stringToInteger = Integer::parselnt;
-> μ μ λ©μλλ₯Ό νΈμΆνλ λλ€ ννμ
2. BiPredicate<List<String>, String> contains = List::contains;
-> 첫λ²μ§Έ μΈμμ contains λ©μλλ₯Ό νΈμΆ
3. Predicate<String> startsWithNumber = this::startsWithNumber;
Classname::new
λ‘ λ§λ€ μ μλ€.
Supplierμ () -> Appleκ³Ό κ°μ μκ·Έλμ²λ₯Ό κ°λ μμ±μκ° μλ€κ³ κ°μ νμ.
Supplier<Apple> c1 = Apple::new;
Apple a1 = c1.get();
Supplier<Apple> c1 = () -> new Apple();
Apple a1 = c1.get();
Apple (integer weight)λΌλ μκ·Έλμ²λ₯Ό κ°λ μμ±μλ Function<T,U> μΈν°νμ΄μ€μ μκ·Έλμ²μ κ°λ€. λ°λΌμ λ€μκ³Ό κ°μ μ½λλ₯Ό ꡬνν μ μλ€.
Function<Integer, Apple> c2 = Apple::new;
Apple a2 = c2.apply(110);
Function<Integer, Apple> c2 = (weight) -> new Apple(weight);
Apple a2 = c2.apply(110);
// μμ
List<Integer> weights = Arrays.asList(7, 3, 4, 10);
List<Apple> apples = map(weights, Apple::new);
public List<Apple> map(List<Integer> list, Function<Integer, Apple) f) {
List<Apple> result = new ArrayList<>();
for(Integer i : list) {
result.add(f.apply(i));
}
return result;
}
Apple (String color, Integer weight) μ²λΌ λ μΈμλ₯Ό κ°λ μμ±μλ BiFunction μΈν°νμ΄μ€μ κ°μ μκ·Έλμ²λ₯Ό κ°μ§λ―λ‘ λ€μκ³Ό κ°μ΄ ν μ μλ€.
BiFunction<Color, Integer, Apple> c3 = Apple::new;
Apple a3 = c3.apply(GREEN, 110);
BiFunction<String, Integer, Apple> c3 =
(color, weight) -> new Apple(color, weight);
Apple a3 = c3.apply(GREEN, 110);
// μμ
static Map<String, Function<Integer, Fruit>> map = new HashMap<>();
static {
map.put("apple", Apple::new);
map.put("orange", Orange::new);
}
public static Fruit giveMeFruit(String fruit, Integer weight) {
return map.get(fruit.toLowerCase())
.apply(weight);
}
Color(int, int, int) μ²λΌ μΈμκ° 3κ°μΈ μμ±μμ μμ±μ μ°Έμ‘°λ?
μ λ΅
Color::new
-> μκ·Έλμ²λ₯Ό μ 곡νλ ν¨μν μΈν°νμ΄μ€κ° νμνλ€.
public interface TriFunction<T, U, V, R> {
R apply(T t, U u, V v);
}
// μ΄μ κ°μ΄ μ¬μ© ν μ μλ€.
TriFunction<Integer, Integer, Integer, Color> colorFactory = Color::new;
λ€μν μ λ ¬ κΈ°λ²μΌλ‘ μ λ ¬νλ λ¬Έμ λ₯Ό ν΄κ²°ν΄λ³΄μ. μ§κΈκΉμ§ λ°°μ΄ λμ νλΌλ―Έν°ν, μ΅λͺ ν΄λμ€, λλ€ ννμ, λ©μλ μ°Έμ‘° λ±μ μ΄λμνλ€.
sortλ λ€μκ³Ό κ°μ μκ·Έλμ²λ₯Ό κ°λλ€.
void sort(Comparator<? super E> c)
@FunctionalInterface
public interface Comparator<T> {
int compare(T o1, T o2);
}
sortμ λμμ νλΌλ―Έν°νλμλ€.
1λ¨κ³λ λ€μκ³Ό κ°μ΄ μμ± ν μ μλ€.
public class AppleComparator implements Comparator<Apple> {
public int compare(Apple a1, Apple a2){
return a1.getWeight().compareTo(a2.getWeight());
}
}
inventory.sort(new AppleComparator());
inventory.sort(new Comparator<Apple>() {
public int compare(Apple a1, Apple a2){
return a1.getWeight().compareTo(a2.getWeight());
}
})οΌ
μΆμ λ©μλμ μκ·Έλμ²(ν¨μ λμ€ν¬λ¦½ν°λΌ λΆλ¦Ό)λ λλ€ ννμμ μκ·Έλμ²λ₯Ό μ μνλ€. Comparatorμ ν¨μ λμ€ν¬λ¦½ν°λ (T, T) -> intλ€.
μ°λ¦¬λ μ¬κ³Όλ₯Ό μ¬μ©ν κ²μ΄λ―λ‘ λ μ ννλ (Apple, Apple) -> intλ‘ ννν μ μλ€.
inventory.sort((Apple a1, Apple a2) ->
a1.getWeight().compareTo(a2.getWeight())
)οΌ
// λ€μκ³Ό κ°μ΄ λ κ°λ¨ν ν μ μλ€.
inventory.sort((a1, a2) -> a1.getWeight().compareTo(a2.getWeight()))οΌ
// μ°μ°λ§ κ°λ¨νλ?
Comparator<Apple> c = Comparator.comparing((Apple a) -> a.getWeight());
inventory.sort(comparing(apple -> apple.getWeight()));
// comparing(μ΄ λ©μλκ° μ μ λ©μλμΈ μ΄μ λ 9μ₯μμ μ€λͺ
ν¨)
public static <T, U extends Comparable<? super U>> Comparator<T> comparing(
Function<? super T, ? extends U> keyExtractor)
{
Objects.requireNonNull(keyExtractor);
return (Comparator<T> & Serializable)
(c1, c2) -> keyExtractor.apply(c1).compareTo(keyExtractor.apply(c2));
}
κ°μ, κΉλ
inventory.sort(comparing(Apple::getWeight));
μλ° 8 APIμ λͺλͺ ν¨μν μΈν°νμ΄μ€λ λ€μν μ νΈλ¦¬ν° λ©μλλ₯Ό ν¬ν¨νλ€. μλ₯Ό λ€μ΄ Comparator, Function, Predicate κ°μν¨μν μΈν°νμ΄μ€λ λλ€ ννμμ μ‘°ν©ν μ μλλ‘ μ νΈλ¦¬ν° λ©μλλ₯Ό μ 곡νλ€.
κ°λ¨ν λ§ν΄, κ°λ¨ν μ¬λ¬ κ°μ λλ€ ννμμ μ‘°ν©ν΄μ 볡μ‘ν λλ€ ννμμ λ§λ€ μ μλ€λ κ²μ΄λ€.
??? ν¨μν μΈν°νμ΄μ€λ νλμ μΆμ λ©μλλ§ μ 곡ν ν
λ° μ΄λ»κ² μ νΈλ¦¬ν° λ©μλλ₯Ό μ 곡νλ€λ κ²μΈκ°?
-> ν΄λ΅μ λν΄νΈ λ©μλ(default method)μ μλ€. (μμΈν λ΄μ©μ 9μ₯μμ λ΄
μλΉ)
inventory.sort(comparing(Apple::getWeight).reversed());
무κ²κ° κ°μ λ μ¬κ³Όκ³Ό μ‘΄μ¬νλ€λ©΄ μ΄λ»κ² ν΄μΌ ν κΉ?
-> λλ²μ§Έ Comparatorμ λ§λ€μ΄μ μ°κ²°ν΄μ£Όμ!
inventory.sort(comparing(Apple::getWeight)
.reversed()
.thenComparing(Apple::getCountry));
Predicate μΈν°νμ΄μ€λ 볡μ‘ν νλ λμΌμ΄νΈλ₯Ό λ§λ€ μ μλλ‘ negate, and, or μΈ κ°μ§ λ©μλλ₯Ό μ 곡νλ€. μλ₯Ό λ€μ΄ βλΉ¨κ°μμ΄ μλ μ¬κ³Όμ²λΌ νΉμ νλ λμΌμ΄νΈλ₯Ό λ°μ μν¬ λ negate
λ©μλλ₯Ό μ¬μ© ν μ μλ€.
Predicate<Apple> notRedApple = redApple.negate();
and
λ©μλλ₯Ό μ΄μ©ν΄μ λΉ¨κ°μμ΄λ©΄μ 무거μ΄μ¬κ³Όλ₯Ό μ ννλλ‘ λ λλ€λ₯Ό μ‘°ν© ν μ μλ€.
Predicate<Apple> redAndHeavyApple =
redApple.and(apple -> apple.getWeight() > 150);
or
μ μ΄μ©ν΄μ βλΉ¨κ°μμ΄λ©΄μ 무거μ΄(150κ·Έλ¨ μ΄μ) μ¬κ³Ό λλ κ·Έλ₯ λ
Ήμ μ¬κ³Όβ
λ± λ€μν 쑰건μ λ§λ€μ μλ€.
Predicate<Apple> redAndHeavyAppleOrGreen =
redApple.and(apple -> apple.getWeight() > 150)
.or(apple -> GREEN.equals(a .getColor()));
andThen, compose λ κ°μ§ λν΄νΈ λ©μλλ₯Ό μ 곡νλ€.
Function<Integer, Integer> f = x -> x + 1;
Function<Integer, Integer> g = x -> x * 2;
Function<Integer, Integer> h = f.andThen(g);
int result = h.apply(1); // 4λ₯Ό λ°ν
-> 머리μ κΌ¬λ¦¬κ° κ°μμΌ λ§€μΉ κ°λ₯!
Function<Integer, Integer> f = x -> x + 1;
Function<Integer, Integer> g = x -> x * 2;
Function<Integer, Integer> h = f.compose(g); // μνμΌλ‘λ f(g(x))λλ (f β g)(x) λΌκ³ νν
int result = h.apply(1); // 3μ λ°ν
μ°Έκ³ μλ£)
λ³Έ ν¬μ€νΈλ λͺ¨λ μλ° μΈ μ‘μ
μ±
μ μ°Έκ³ νμ¬ μμ±νμμ΅λλ€.