반응형
Functional Interface
- Abstract Method가 하나인 인터페이스
- 람다식 사용이 가능
- FunctionalInterface annotation을 안붙여도 되지만, 붙이는 것을 권장
Java SDK 8의 java.util.function 패키지에는 수많은 Functional Interface들이 등록있다.
대표적인 finctional interface를 구현해본 코드
Cunsumer
import java.util.function.Consumer;
public class ConsumerFunction {
private static void consumerExamples() {
final Consumer<String> print = value -> System.out.println(value);
print.accept("Hello");
final Consumer<String> greetings = value -> System.out.println("Hello " + value);
greetings.accept("World");
greetings.accept("JungHoon");
}
public static void main(final String[] args) {
consumerExamples();
}
}
Identify
public class IdentityFunction {
public static void main(String[] args){
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5 , 6);
System.out.println("map (numbers, i -> i * 2)");
System.out.println(map(numbers, i -> i * 2));
// map 기능이 필요 없을 때 null로 구분하고 싶다
System.out.println("map (numbers, null)");
System.out.println(map(numbers, i -> i));
System.out.println(map(numbers, Function.identity()));
}
private static <T, R> List<R> map(final List<T> list, final Function<T, R> mapper){
List<R> result = new ArrayList<>();
for(final T t : list){
result.add(mapper.apply(t));
}
return result;
}
}
Supplier
public class LazyEvaluationSupplier {
public static void main(String[] args){
long startTime = System.currentTimeMillis();
printIfValidIndex(0, getVeryExpensiveValue());
printIfValidIndex(-1, getVeryExpensiveValue());
printIfValidIndex(-3, getVeryExpensiveValue());
System.out.println("It took " + ((System.currentTimeMillis() - startTime) / 1000));
startTime = System.currentTimeMillis();
printIfValidIndexWithSupplier(0, () -> getVeryExpensiveValue());
printIfValidIndexWithSupplier(-1, () -> getVeryExpensiveValue());
printIfValidIndexWithSupplier(-3, () -> getVeryExpensiveValue());
System.out.println("It took " + ((System.currentTimeMillis() - startTime) / 1000));
}
private static String getVeryExpensiveValue() {
try{
TimeUnit.SECONDS.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "PJH";
}
//number 조건이 맞지 않아도 getVeryExpensiveValue method를 parameter 로 전달하기 위해 호출함
private static void printIfValidIndex(int number, String value){
if(number >= 0)
System.out.println("value : " + number);
else
System.out.println("Invalid");
}
//number 조건이 맞지않을때 불필요한 getVeryExpensiveValue method 호출을 하지 않음
private static void printIfValidIndexWithSupplier(int number, Supplier<String> valueSupplier){
if(number >= 0)
System.out.println("value : " + number);
else
System.out.println("Invalid");
}
}
Predicate
public class PredicateFunction {
public static void main(String[] args){
Predicate<Integer> lessThan3 = number -> number < 3;
Predicate<Integer> moreThan3 = number -> number > 3;
List<Integer> IntegerArray = Arrays.asList(1,2,3,4,1,2,3,5,6,3,1,2);
filter(IntegerArray, lessThan3).forEach( i -> System.out.print(i + " "));
System.out.println("");
filter(IntegerArray, moreThan3).forEach( i -> System.out.print(i + " "));
}
private static <T> List<T> filter(List<T> list, Predicate<T> filterFunction){
List<T> resultArrays = new ArrayList<>();
for(T value : list){
if(filterFunction.test(value)) {
resultArrays.add(value);
}
}
return resultArrays;
}
}
반응형