본문 바로가기

함수형 인터페이스3

Java - 함수형 인터페이스(Predicate) 합성 and(), or(), negate()로 두 Predicate를 하나로 합성할 수 있다. Predicate p = i -> i i i%2 == 0; Predicate notP = p.negate(); // i >= 10 Predicate all = notP.and(q).or(r); // 10 2017. 3. 1.
Java - 자바에서 제공하는 함수형 인터페이스 자주 사용되는 함수형 인터페이스를 제공 인터페이스 메서드 설명 java.lang.Runnable void run() 리턴 값과 매개변수 둘다 없다. Supplier T get() 리턴 값만 있다. Consumer void accept(T t) 매개변수만 있다. Function R apply(T t) 하나의 매개변수와 하나의 리턴 값을 가진다. Predicate boolean test(T t) 하나의 매개변수를 가지고 booean값을 리턴한다. import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; pub.. 2017. 2. 26.
Java - 함수형 인터페이스 단 하나의 추상 메서드만 선언된 인터페이스 interface ClassName { public abstract int sum(int a, int b); } ClassName cn = new ClassName() { // 기존에는 아래와 같이 new 함수를 사용하여 객체를 생성할 때 함수를 구현한다. public int sum(int a, int b) { return a + b; } }; // 함수를 사용한다.int c = cn.sum(1, 2); //람다식 사용하기 ClassName cn2 = (a, b) -> a + b cn2.sum(1, 2); 함수형 인터페이스를 매개변수로 받기 int test(ClassName cn) { // 고차원함수 reuturn cn.sum(1, 2); } test(cn2);.. 2017. 2. 25.