개발 공부/Java
[Java] 자바에서 제공하는 함수형 인터페이스
upcake_
2021. 8. 24. 20:44
반응형
https://www.inflearn.com/course/the-java-java8#
백기선 개발자님의 더 자바, Java 8 강의를 들으면서 정리한 내용입니다.
public class Chap02 {
public static void main(String[] args) {
//인터페이스 객체를 선언하여 사용하는법
Plus10 plus10 = new Plus10();
//만든 Function 인터페이스는 apply로 사용한다
System.out.println(plus10.apply(1));
//바로 함수형 인터페이스를 사용하는 법
Function<Integer, Integer> plus10_2 = (number) -> number + 10;
Function<Integer, Integer> multiply2 = (i) -> i * 2;
//compose는 입력값을 가지고 파라미터함수를 먼저 적용하고 그 결과값을 다시 본래의 함수를 적용한다
Function<Integer, Integer> multiply2AndPlus10 = plus10_2.compose(multiply2);
System.out.println(multiply2AndPlus10.apply(1));
//andThen은 뒤에다가 붙임
Function<Integer, Integer> plus10AndMultiply2 = plus10_2.andThen(multiply2);
System.out.println(plus10AndMultiply2.apply(1));
/*------------------------------------------------- */
//Consumer는 리턴값이 없이 파라미터로 동작을 수행하는 인터페이스
Consumer<Integer> printT = (i) -> System.out.println(i);
printT.accept(10);
//Supplier는 어떤 값을 받아올것인가
Supplier<Integer> get10 = () -> 10;
System.out.println(get10.get());
//Predicate 어떤 값을 받아서 true false를 리턴하는 함수
//and or 등으로 조합가능
Predicate<String> startsWithUpcake = (s) -> s.startsWith("upcake");
Predicate<Integer> isEven = (i) -> i%2 == 0;
//UnaryOperator Function과 비슷하지만 입력타입과 출력타입이 같을 경우 사용
UnaryOperator<Integer> plus10_3 = (i) -> i +10;
}
}
반응형