java817 Java - 함수형 인터페이스(Function) 합성 andThen, compose Function f = (s) -> Integer.parseInt(s); Function f2 = (i) -> i.toString() + "dddd"; Function result = f.andThen(f2); // f + f2 → result Function result2 = f2.compose(f); // f + f2 → result System.out.println(result.apply("99")); //99dddd System.out.println(result2.apply("99")); //99dddd 2017. 2. 28. Java - 기본형을 사용하는 함수형 인터페이스 인터페이스 메서드 설명 DoubleToIntFunction int applyAsInt(double d) aTobFunction a는 입력 b는 리턴 ToIntFunction int applyAsInt(T d) ToaFunction a는 입력 리턴은 제네릭 IntFunction T applyAsInt(int d) aFunction 리턴이 제네릭 ObjIntCunsumer void applyAsInt(T d, int i) 입력에 제네릭, int 리턴은 없다 Supplier s = ()->(int)(Math.random()*100); static void makeRandomList(Supplier s, List list) { for(int i=0;i(int)(Math.random()*100); static v.. 2017. 2. 27. 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. 이전 1 2 3 4 5 다음