public class FunctionTest implements Function<Integer , Integer> { @Override public Integer apply(Integer integer) { return integer + 100; } }
public static void main(String[] args) { // 객체 생성 FunctionTest test1 = new FunctionTest(); System.out.println("객체 생성 : " + test1.apply(10)); // 람다 표현식 Function<Integer , Integer> add = (i) -> i + 10; System.out.println("람다 표현식 (더하기) : " + add.apply(10)); Function<Integer , Integer> multi = (i) -> i * 20; System.out.println("람다 표현식 (곱하기) : " + multi.apply(10)); // 곱하기를 수행한 후 더하기를 수행한 값을 리턴한다. // 10 * 20 + 10 Function<Integer , Integer> compose = add.compose(multi); System.out.println("람다 표현식 (compose) : " + compose.apply(10)); // 더하기를 수행 한 후 곱하기를 수행한 값을 리턴한다. // 10 + 10 * 20 Function<Integer , Integer> andThen = add.andThen(multi); System.out.println("람다 표현식 (andThen) : " + andThen.apply(10)); // 출력 // 객체 생성 : 110 // 람다 표현식 (더하기) : 20 // 람다 표현식 (곱하기) : 200 // 람다 표현식 (compose) : 210 // 람다 표현식 (andThen) : 400 }
public static void main(String[] args) { Supplier<Integer> get = () -> 1; System.out.println(get.get()); // 출력 // 1 }
public static void main(String[] args) { Predicate<String> startsWithTis = (s) -> s.startsWith("t"); Predicate<Integer> isEven = (s) -> s%2 == 0; System.out.println("startsWithTis : " + startsWithTis.test("istory")); System.out.println("isEven : " + isEven.test(10)); System.out.println("isEven (negate) : " + isEven.negate().test(10)); // 출력 // startsWithTis : false // isEven : true // isEven (negate) : false }
public static void main(String[] args) { UnaryOperator<Integer> add = (i) -> i + 10; System.out.println("UnaryOperator (더하기) : " + add.apply(10)); UnaryOperator<Integer> multi = (i) -> i * 20; System.out.println("UnaryOperator (곱하기) : " + multi.apply(10)); // 출력 // UnaryOperator (더하기) : 20 // UnaryOperator (곱하기) : 200 }
<? extends T>T와 그 자손 타입만 가능(upper bound)<? super T>T와 그 조상 타입만 가능(lower bound)<?>제한 없이 모든 타입이 가능. <? extneds Object>와 동일한 표현
더 자바, Java 8 - 인프런
자바 8에 추가된 기능들은 자바가 제공하는 API는 물론이고 스프링 같은 제 3의 라이브러리 및 프레임워크에서도 널리 사용되고 있습니다. 이 시대의 자바 개발자라면 반드시 알아야 합니다. 이
www.inflearn.com
* 단축키는 한글/영문 대소문자로 이용 가능하며, 티스토리 기본 도메인에서만 동작합니다.
[JAVA8] 자바에서 제공하는 함수형 인터페이스
Java가 기본으로 제공하는 함수형 인터페이스
public class FunctionTest implements Function<Integer , Integer> { @Override public Integer apply(Integer integer) { return integer + 100; } }
public static void main(String[] args) { // 객체 생성 FunctionTest test1 = new FunctionTest(); System.out.println("객체 생성 : " + test1.apply(10)); // 람다 표현식 Function<Integer , Integer> add = (i) -> i + 10; System.out.println("람다 표현식 (더하기) : " + add.apply(10)); Function<Integer , Integer> multi = (i) -> i * 20; System.out.println("람다 표현식 (곱하기) : " + multi.apply(10)); // 곱하기를 수행한 후 더하기를 수행한 값을 리턴한다. // 10 * 20 + 10 Function<Integer , Integer> compose = add.compose(multi); System.out.println("람다 표현식 (compose) : " + compose.apply(10)); // 더하기를 수행 한 후 곱하기를 수행한 값을 리턴한다. // 10 + 10 * 20 Function<Integer , Integer> andThen = add.andThen(multi); System.out.println("람다 표현식 (andThen) : " + andThen.apply(10)); // 출력 // 객체 생성 : 110 // 람다 표현식 (더하기) : 20 // 람다 표현식 (곱하기) : 200 // 람다 표현식 (compose) : 210 // 람다 표현식 (andThen) : 400 }
public static void main(String[] args) { Supplier<Integer> get = () -> 1; System.out.println(get.get()); // 출력 // 1 }
public static void main(String[] args) { Predicate<String> startsWithTis = (s) -> s.startsWith("t"); Predicate<Integer> isEven = (s) -> s%2 == 0; System.out.println("startsWithTis : " + startsWithTis.test("istory")); System.out.println("isEven : " + isEven.test(10)); System.out.println("isEven (negate) : " + isEven.negate().test(10)); // 출력 // startsWithTis : false // isEven : true // isEven (negate) : false }
public static void main(String[] args) { UnaryOperator<Integer> add = (i) -> i + 10; System.out.println("UnaryOperator (더하기) : " + add.apply(10)); UnaryOperator<Integer> multi = (i) -> i * 20; System.out.println("UnaryOperator (곱하기) : " + multi.apply(10)); // 출력 // UnaryOperator (더하기) : 20 // UnaryOperator (곱하기) : 200 }
+
<? extends T>
T와 그 자손 타입만 가능(upper bound)
<? super T>
T와 그 조상 타입만 가능(lower bound)
<?>
제한 없이 모든 타입이 가능. <? extneds Object>와 동일한 표현
더 자바, Java 8 - 인프런
자바 8에 추가된 기능들은 자바가 제공하는 API는 물론이고 스프링 같은 제 3의 라이브러리 및 프레임워크에서도 널리 사용되고 있습니다. 이 시대의 자바 개발자라면 반드시 알아야 합니다. 이
www.inflearn.com
'기록 > JAVA' 카테고리의 다른 글