ํ‘œ์ค€ API์˜ ํ•จ์ˆ˜ํ˜• ์ธํ„ฐํŽ˜์ด์Šค

์ข…๋ฅ˜ ์ถ”์ƒ ๋ฉ”์†Œ๋“œ์˜ ํŠน์ง•
Consumer ๋งค๊ฐœ๊ฐ’์€ ์žˆ๊ณ , ๋ฆฌํ„ด๊ฐ’์€ ์—†์Œ
Supplier ๋งค๊ฐœ๊ฐ’์€ ์—†๊ณ , ๋ฆฌํ„ด๊ฐ’์€ ์žˆ์Œ
Function

๋งค๊ฐœ๊ฐ’๋„ ์žˆ๊ณ , ๋ฆฌํ„ด๊ฐ’๋„ ์žˆ์Œ

์ฃผ๋กœ ๋งค๊ฐœ๊ฐ’์„ ๋ฆฌํ„ด๊ฐ’์œผ๋กœ ๋งคํ•‘(ํƒ€์ž… ๋ณ€ํ™˜)

Operator

๋งค๊ฐœ๊ฐ’๋„ ์žˆ๊ณ , ๋ฆฌํ„ด๊ฐ’๋„ ์žˆ์Œ

์ฃผ๋กœ ๋งค๊ฐœ๊ฐ’์„ ์—ฐ์‚ฐํ•˜๊ณ  ๊ฒฐ๊ณผ๋ฅผ ๋ฆฌํ„ด

Predicate

๋งค๊ฐœ๊ฐ’๋„ ์žˆ๊ณ , ๋ฆฌํ„ดํƒ€์ž…์€ boolean

๋งค๊ฐœ๊ฐ’์„ ์กฐ์‚ฌํ•˜๊ณ  true/false๋ฅผ ๋ฆฌํ„ด

Consumer

public class Example {
  public static void main(String[] args) {
    Consumer<String> consumer = t -> System.out.println(t);
    consumer.accept("test");

    BiConsumer<String, String> biConsumer = (t, u) -> System.out.println(t + u);
    biConsumer.accept("te", "st");

    DoubleConsumer doubleConsumer = d -> System.out.println(d);
    doubleConsumer(3.2);

    ObjIntConsumer<String> objIntConsumer = (t, i) -> System.out.println(t + i);
    objIntConsumer("test", 1);
  }
}

Supplier

public class Example {
  public static void main(String[] args) {
    IntSupplier intSupplier = () -> {
      return (int) (Math.random * 100) + 1;
    };

    System.out.println(intSupplier.getAsInt());
  }
}

Function

public class Member {
  private int id;
  private String name;

  public Member(int id, String name) {
    this.id = id;
    this.name = name;
  }

  public int getId() { return id };
  public String getName() { return name };
}

public class Example1 {
  private static List<Member> members = Arrays.asList(new Member(1, "m1"), new Member(2, "m2"));

  public static void printString( Function<Member, String> function ) {
    for(Member member : members) {
      System.out.println(function.apply(member));
    }
  }

  public static int printInt( ToIntFunction<Member> function ) {
    for(Member member : members) {
      System.out.println(fuction.applyAsInt(member));
    }
  }

  public static void main(String[] args) {
    printString(t -> t.getName());
    printInt(t -> t.getId());
  }
}
public class Member {
  private int id;
  private String name;

  public Member(int id, String name) {
    this.id = id;
    this.name = name;
  }

  public int getId() { return id };
  public String getName() { return name };
}

public class Example2 {
  private static List<Member> members = Arrays.asList(new Member(1, "m1"), new Member(2, "m2"));

  public static int sum(ToIntFunction<Member> function) {
    int sum = 0;

    for(Member member : members) {
      sum += funtion.applyAsInt(member);
    }

    return sum;
  }

  public static void main(String[] args) {
    printString(t -> t.getId());
  }
}

Operator

public class Example {
  private static int[] list = { 10, 20, 30 };

  public static int maxOrMin( IntBinaryOperator operator ) {
    int result = list[0];
    for(int i : list) {
      result = operator.applyAsInt(result, i);
    }

    return result;
  }

  public static void main(String[] args) {
    System.out.println(maxOrMin((a, b) -> Math.max(a, b)));
    System.out.println(maxOrMin((a, b) -> Math.min(a, b)));
  }

}

Predicate

public class Member {
  private String name;
  private int score;
  private String gender;

  public Member(String name, int score, String gender) {
    this.name = name;
    this.score = score;
    this.gender = gender;
  }

  public String getName() {
    return name;
  }

  public int score() {
    return score
  }

  public String getGender() {
    return gender;
  }
}

public class Example {
  private static List<Member> members = Arrays.asList(
    new Member("m1", 20, "male"),
    new Member("m2", 16, "female"),
    new Member("m3", 47, "male"),
    new Member("m4", 61, "female")
  );

  public static double avg( Predicate<Membber> predicate) {
    int count = 0, sum = 0;

    for(Member member : members) {
      if(predicate.test(member)) {
        ++count;
        sum += member.getScore();
      }
    }

    return (double) sum / count;
  }

  public static void main(String[] args) {
    System.out.println(avg(t -> t.getGender().eqauls("male")));
    System.out.println(avg(t -> t.getGender().eqauls("female")));
  }
}

andThen()๊ณผ compose() ๋””ํดํŠธ ๋ฉ”์†Œ๋“œ

InterfaceAB interfaceAB = interfaceA.andThen(interfaceB);
intrefaceAB.method();
andthen
andThen
InterfaceAB interfaceAB = interfaceA.compose(interfaceB);
intrefaceAB.method();
compose
compose

Consumer์˜ ์ˆœ์ฐจ์  ์—ฐ๊ฒฐ

public class Member {
  private int id;
  private String name;
  private Address address;

  public Member(int id, String name, Address address) {
    this.id = id;
    this.name = name;
    this.address = address;
  }

  public int getId() { return id; }
  public String getName() { return name; }
  public Address getAddress() { return addresss };
}

public class Address {
  private String country;
  private String city;

  public Address(String country, String) {
    this.country = country;
    this.city = city;
  }

  public String getCountry() { return country; }
  public String getCity() { return city; }
}

public class Example {
  public static void main(String[] args) {
    Consumer<Member> consumerA = m -> System.out.println(m.getName());
    Consumer<Member> consumerB = m -> System.out.println(m.getId());

    Consumer<Member> consumerAB = consumerA.andThen(consumerB);
    consumerAB.accept(new Member(1, "member", null));
  }
}

Function์˜ ์ˆœ์ฐจ์  ์—ฐ๊ฒฐ

public class Example {
  public static void main(String[] args) {
    Function<Member, Address> functionA;
    Function<Address, String> functionB;
    Function<Member, String> functionAB;

    functionA = m -> m.getAddress();
    functionB = a -> a.getCity();

    functionAB = functionA.andThen(functionB);
    System.out.println(functionAB.apply(new Member(1, "member", new Address("korea", "seoul"))));

    functionAB = functionB.compose(functionA);
    System.out.println(functionAB.apply(new Member(1, "member", new Address("korea", "seoul"))));
  }
}

and(), or(), negate() ๋””ํดํŠธ ๋ฉ”์†Œ๋“œ์™€ isEqual() static ๋ฉ”์†Œ๋“œ

public class Example {
  public static void main(String[] args) {
    IntPredicate predicateA = a -> a & 2 == 0;
    IntPredicate predicateB = b -> b & 3 == 0;

    IntPredicate predicateAB;
    predicateAB = predicateA.and(predicateB);
    System.out.println(predicateAB.test(9));

    predicateAB = predicateA.or(predicateB);
    System.out.println(predicateAB.test(9));

    predicateAB = predicateA.negate();
    System.out.println(predicateAB.test(9));
  }
}

๋ฉ”์†Œ๋“œ ์ฐธ์กฐ(Method Reference)

์˜ˆ์‹œ

(n1, n2) -> Math.max(n1, n2);

// ์œ„์™€ ๊ฐ™์Œ
Math::max

// IntBinaryOperator ๋Š” ๋‘ ๊ฐœ์˜ int ๊ฐ’์„ ๋ฐ›์•„ int๋ฅผ ๋ฆฌํ„ดํ•˜๋ฏ€๋กœ Math::max ๋ฉ”์†Œ๋“œ ์ฐธ์กฐ ๋Œ€์ž… ๊ฐ€๋Šฅ
IntBinaryOperator operator = (n1, n2) -> Math.max(n1, n2);

IntBinaryOperator operator = Math::max // ์œ„์™€ ๊ฐ™์Œ

static ๋ฉ”์†Œ๋“œ์™€ ์ธ์Šคํ„ด์Šค ๋ฉ”์†Œ๋“œ ์ฐธ์กฐ

public class Calculator {
  public static int sum(int x, int y) {
    return x + y;
  }

  public int multiply(int x, int y) {
    return x * y;
  }
}

public class Example {
  public static void main(String[] args) {
    IntBinaryOperator operator;

    operator = (x, y) -> Calculrator.sum(x, y);
    System.out.println(operator.applyAsInt(2, 4));

    operator = Calculator::sum;
    System.out.println(operator.applyAsInt(3, 4));

    Calculator calculator = new Calculator();

    operator = (x, y) -> calculator.multiply(x, y);
    System.out.println(operator.applyAsInt(2, 4));

    operator = calculator::multiply;
    System.out.println(operator.applyAsInt(2, 4));
  }
}

๋งค๊ฐœ ๋ณ€์ˆ˜์˜ ๋ฉ”์†Œ๋“œ ์ฐธ์กฐ

public class Example {
  public static void main(String[] args) {
    ToIntBinaryFunction<String, String> function;

    function = (a, b) -> a.compareToIgnoreCase(b);
    System.out.println(function.applyAsInt("test", "TEST")); // 0

    function = String::compareToIgnoreCase;
    System.out.println(function.applyAsInt("test", "TEST")); // 0
  }
}

์ƒ์„ฑ์ž ์ฐธ์กฐ

public class Member {
  private String id;
  private String name;

  public Member() {
    System.out.println("Member()");
  }

  public Member(String id) {
    System.out.println("Member(String)");
    
    this.id = id;
  }

  public Member(String id, String name) {
    System.out.println("Member(String, String)");

    this.id = id;
    this.name = name;
  }

  public String getId() {
    return id;
  }
}

public class Example {
  public static void main(String[] args) {
    Function<String, Member> function1 = Member::new;
    Member member1 = function1.apply("m1");

    BiFunction<String, String, Member> function2 = Member::new;
    Member member2 = function2.apply("m2", "test");
  }
}