본문 바로가기
교육/Java

Java 개발자 수업 90일차 - Function, Operation, Predicate, 함수 조합용 메소드

by yhyuk 2021. 8. 7.
728x90
반응형

1. Function

2. Operation

3. Predicate

4. 함수 조합용 메소드


1. Function

[ 정의 ]

- 매개변수를 반환값으로 변환 후 반환

- applyXXX() 메소드를 제공한다.

- 매개변수O, 반환값O

- Function<T>, BiFunction<T, R>

 

[ 예제 - Function, BiFunction ]

// 1. Function
Function<String, Integer> f1 = str.length();
System.out.println(f1.apply("홍길동"));

Function<Integer, String> f2 = num -> num > 0 ? "양수" : "음수 or 0";
System.out.println(f2.apply(100));
System.out.println(f2.apply(-100));

// 2. BiFunction
BiFunction<Integer, Integer, Integer> bf1 = (a, b) -> a + b;
System.out.println(bf1.apply(20, 30));

BiFunction<Integer, Integer, Boolean> bf2 = (a, b) -> a > b;
System.out.println(bf2.apply(20, 10));
System.out.println(bf2.apply(10, 20));


--> Function Output
3
양수
음수 or 0

--> BiFunction Output
50
true
false

 

2. Operation

- 매개변수를 연산 후 결과 반환 (Function 하위 버전)

- applyXXX() 메소드를 제공한다.

- Operator는 Function과 다르게 매개변수의 타입과 반환값의 타입이 동일하다. 

- 매개변수O, 반환값O

- BinaryOperator<T>

 

[ 예제 - BiFunction vs BinaryOperator  ]

// 1. BiFunction
BiFunction<Integer,	Integer, Integer> bf1 = (a, b) -> a * b;
System.out.println(bf1.apply(10, 20));

// 2. BinaryOperator
BinaryOperator<Integer> bo1 = (a, b) -> a * b;
System.out.println(bo1.apply(10, 20));


--> BiFunction Output
200

--> BinaryOperator Output
200

 

3. Predicate

- 매개변수의 값을 받아 무언가 조사한 후 논리값을 반환한다.

- testXXX() 메소드를 제공한다.

- 매개변수O, 반환값O

- Predicate<T>, BiPredicate<T, U>

 

[ 예제 - Predicate, BiPredicate ]

// 1. Predicate
Predicate<Integer> p1 = n -> n > 0;
if (p1.test(10)) { 
    System.out.println("양수");
} else {
    System.out.println("음수 or 0");
}

// 2. BiPredicate
BiPredicate<Integer, Integer> bp1 = (a, b) -> a > b;
System.out.println(bp1.test(20, 10));



--> Predicate Output
양수


--> BiPredicate Output
true

 

4. 함수 조합용 메소드

  1) andthen()

  - 이미 존재하는 람다식을 순서에 따라 연속적으로 호출할 수 있도록 결합해주는 역할

  - A.andThen(B) : A -> B

  - 예제)

// 예제1) 2개의 Consumer c1, c2가 존재할 때 두개의 업무를 같이 실행하기 
Consumer<Integer> c1 = num -> System.out.println(num > 0 ? "양수" : "음수 or 0");
Consumer<Integer> c2 = num -> System.out.println(num % 2 == 0 ? "짝수" : "홀수");
Consumer<Integer> c3 = c1.andThen(c2);
c3.accept(100);


// 예제2) 2개의 Function f1, f2가 존재할 때 두개의 업무를 같이 실행하기
Function<Integer, Boolean> f1 = num -> num > 0;
Function<Boolean, String> f2 = flag -> flag ? "양수" : "음수";
Function<Integer, String> f3 = f1.andThen(f2);
System.out.println(f3.apply(10));


--> 예제1) Consumer Output
양수
짝수

--> 예제2) Function Output
양수

 

  2) compose()

  - andThen()의 반대로 이전 메소드를 실행 후 결과를 받아서 현재 메소드를 실행한다.

  - A.compose(B) : B -> A

  - 예제)

Function<Integer, Boolean> f1 = num -> num > 0;
Function<Boolean, String> f2 = flag -> flag ? "양수" : "음수";
Function<Integer, String> f3 = f2.compose(f1);

System.out.println(f3.apply(10));


--> Output
양수

 

  3) and(), or(), negate()

  - and() 메소드: A.and(B) > A의 결과 && B의 결과

  - or() 메소드:  A.or(B) > A의 결과 || B의 결과

  - negate() 메소드: A.negdate() > A의 결과의 반대, !A

  - 예제)

// 1. and()
Predicate<Integer> p1 = num -> num % 2 == 0; // 2의 배수 
Predicate<Integer> p2 = num -> num % 3 == 0; // 3의 배수
Predicate<Integer> p3 = p1.and(p2); // p1의 결과 && p2의 결과

System.out.println(p3.test(6)); // 2의 배수 & 3의 배수 -> true
System.out.println(p3.test(5)); // 2의 배수 & 3의 배수 -> false


// 2. or()
Predicate<Integer> p4 = p1.or(p2); // p1의 결과 || p2의 결과
System.out.println(p4.test(2)); // 2의 배수 or 3의배수 -> true
System.out.println(p4.test(3)); // 2의 배수 or 3의배수 -> true
System.out.println(p4.test(5)); // 2의 배수 or 3의배수 -> false

// 3. negate()
Predicate<Integer> p5 = p1.negate(); // !p1
System.out.println(p5.test(2)); // 2의배수 -> true -> 부정 -> false
System.out.println(p5.test(3)); // 2의배수 -> false -> 부정 -> true

--> and() Output
true
false

--> or() Output
true
true
false

--> negate() Output
false
true

MEMO>

# 함수 조합 메소드에서 compose()는 before, andThen()은 after 형식으로 기억하자.

# 람다식 자체가 생소한 문법이며, 처음보는 인터페이스, 메소드가 많으므로 적응 하도록 하자.

 

 

728x90
반응형

댓글