[Java8] JAVA가 제공하는 기본 함수형 인터페이스 정리

 

 

 

[Java8] JAVA가 제공하는 기본 함수형 인터페이스

 

 

함수형 인터페이스(Functional Interface)

함수형 인터페이스를 직접 만들어 사용할 수도 있지만

이미 JAVA가 제공해주는 기본 함수 인터페이스를 사용할 수 있습니다.

 


 

 

Function

 

 

Function<T, R>

apply : 결과 반환

A.compose(B) : 조합용 함수

A.andThen(B)  : 조합용 함수

 

T 타입을 받아서 R 타입을 리턴하는 함수 인터페이스

입력 type과 반환 type이 다를 수 있습니다.

 

Class 형태로 제작 후 사용

public class Plus10Functional implements Function<Integer, Integer> {
    @Override
    public Integer apply(Integer integer) {
        return integer + 10;
    }
}

class명 뒤에 implements를 붙여서 제작할 수 있습니다.

Plus10Functional Plus10Functional = new Plus10Functional();
System.out.println(Plus10Functional.apply(1)); //11

사용할때는 일반적 class를 사용하듯이 new 형태로 선언하여 사용하면 됩니다.

 

 

함수형 인터페이스를 직접 사용한 형태

Function<Integer, Integer> plus11 = (i) -> {
    return i + 11;
};
System.out.println(plus11.apply(1)); //12

함수형 인터페이스를 직접 불러서 사용한 형태입니다.

Function<Integer, Integer> plus12 = (i) -> i + 12;
System.out.println(plus12.apply(1)); //13

위와 같이 람다식 형태로 줄여서도 사용 가능합니다.

 

입력과 출력이 다른 형태

Function<String, Integer> stringLengthFunction = (str) -> str.length();
System.out.println(stringLengthFunction.apply("Hello")); // 5

 

 

A.compose(B) 함수 조합용 메소드 : B→A

Function<Integer, Integer> plus10 = (i) -> i + 10;
Function<Integer, Integer> multiply2 = (i) -> i * 2; //곱하기

Function<Integer, Integer> multiply2AndPlus10 = plus10.compose(multiply2); 
System.out.println(multiply2AndPlus10.apply(2)); // (2 * 2) + 10 = 14

2개의 Function 함수 인터페이스를 조합해서 사용 가능합니다.

Function<Integer, Integer> plus10 = (i) -> i + 10;
Function<Integer, Integer> multiply2 = (i) -> i * 2; //곱하기

System.out.println(plus10.compose(multiply2).apply(2)); // 함수형으로 선언 없이 사용

compose 조합용 메소드 또한 람다식 형태로도 사용 가능합니다.

 

 

 

A.andThen(B) 함수 조합용 메소드 : A→B

Function<Integer, Integer> plus10 = (i) -> i + 10;
Function<Integer, Integer> multiply2 = (i) -> i * 2;

Function<Integer, Integer> multiply2AndThenPlus10 = plus10.andThen(multiply2); 
System.out.println(multiply2AndThenPlus10.apply(2)); // (10 + 2) * 2 = 24

2개의 Function 함수 인터페이스를 조합해서 사용 가능합니다.

Function<Integer, Integer> plus10 = (i) -> i + 10;
Function<Integer, Integer> multiply2 = (i) -> i * 2;

System.out.println(plus10.andThen(multiply2).apply(2)); // (10 + 2) * 2 = 24

andThen 조합용 메소드 또한 람다식 형태로도 사용 가능합니다.

 

 


 

 

BiFunction

 

 

BiFunction<T, U, R>

apply : 결과 반환

 

T 타입을 받아서 R 타입을 리턴하는 함수 인터페이스

입력 type과 반환 type이 다를 수 있습니다.

java.util.function.BiFunction<Integer, Integer, Integer> plus_plus10 = (i, j) -> i + j + 10;
System.out.println(plus_plus10.apply(1,1)); //12

Function과 다른 점은 BiFunction은 Parameter 2개를 받아서 return 하는 함수입니다.

 

 

 


 

 

Consumer<T>

 

Consumer<T>

accept : 결과 반환

andThen : 함수 조합용 메소드

T 타입을 받아서 처리하고, 결과를 반환하지 않는 함수형 인터페이스

 

Consumer<Integer> printT1 = (i) -> System.out.println(i);
Consumer<Integer> printT2 = (i) -> System.out.println(i);

printT1.accept(10); //10
printT2.accept(20); //20

printT1.andThen(printT2).accept(10);
//10
//10

Consumer<받는 Parameter type> 형태로 작성하여야 합니다.

return 값이 없다는것이 가장 큰 특징입니다.

 

 


 

Supplier<T>

 

 

Supplier<T>

매개변수를 받지 않고 T 타입의 값을 결과로 반환하는 함수형 인터페이스

 

Supplier<Integer> get10 = () -> 10;
System.out.println(get10); //10

 

함수 사용 할 때, 반환할 type의 값을 넣으면 됩니다.

람다식에 넘기는 인자값이 없습니다.

 


 

Predicate<T>

 

 

Predicate<T>

test : 결과 반환

A.and(B) : 함수 조합용 메소드
A.or(B) : 함수 조합용 메소드
A.negate(B) : 함수 조합용 메소드( 논리적 NOT 연산 )

 

T 타입을 받아서 boolean을 리턴하는 함수 인터페이스

 

Predicate<String> startWithJoy = (s) -> s.startsWith("joy");
System.out.println(startWithJoy.test("joy"));       //true
System.out.println(startWithJoy.test("Not joy"));   //false

Predicate<Integer> isEven = (i) -> i%2 == 0; //Even 짝수, Odd 홀수
Predicate<Integer> isGreaterThan10 = (number) -> number > 10; //10보다 큰지 확인
System.out.println(isEven.test(10));          //true
System.out.println(isGreaterThan10.test(10)); //false

Predicate는 boolean을 반환한다는 것이 가장 큰 특징입니다.

 

 

A.and(B) : 함수 조합용 메소드

//함수형 선언 후 사용
Predicate<Integer> isEven = (i) -> i%2 == 0; //Even 짝수, Odd 홀수
Predicate<Integer> isGreaterThan10 = (number) -> number > 10; //10보다 큰지 확인

Predicate<Integer> isEvenAndOdd = isEven.and(isGreaterThan10);
System.out.println(isEvenAndOdd.test(10));  //false

//함수형 선언 없이 바로 사용
System.out.println(isEven.and(isGreaterThan10).test(10)); //false

a,b 둘다 true 일때 true로 반환 됩니다.

 

A.or(B) : 함수 조합용 메소드

//함수형 선언 후 사용
Predicate<Integer> isEven = (i) -> i%2 == 0; //Even 짝수, Odd 홀수
Predicate<Integer> isGreaterThan10 = (number) -> number > 10; //10보다 큰지 확인

Predicate<Integer> isEvenOrOdd = isEven.or(isGreaterThan10);
System.out.println(isEvenOrOdd.test(10));  //true

//함수형 선언 없이 바로 사용
System.out.println(isEven.or(isGreaterThan10).test(10)); //true

a,b 둘중 하나만 true 라도 true로 반환 됩니다.

 

 

A.negate() : 함수 조합용 메소드 

//함수형 선언 후 사용
Predicate<Integer> isEven = (i) -> i%2 == 0; //Even 짝수, Odd 홀수
Predicate<Integer> isOdd = isEven.negate(); //negate 부정하다
System.out.println(isOdd.test(7)); // true (7는 홀수)

//함수형 선언 없이 바로 사용
System.out.println(isEven.negate().test(7)); // true

negate는 논리적 NOT 연산 입니다.

결과 값이 거짓일때 true로 반환됩니다.

 

 


 

 

UnaryOperator

 

UnaryOperator<T>

apply : 결과 반환

A.compose(B) : 조합용 함수

A.andThen(B)  : 조합용 함수

 

Function<T, R>의 특수한 형태로, 입력값 하나를 받아서 동일한 타입을 리턴하는 함수 인터페이스

입력 type과 반환 type이 같습니다.

 

UnaryOperator<Integer> plus10 = (i) -> i + 10;
UnaryOperator<Integer> multiply2 = (i) -> i * 2;

System.out.println(plus10.apply(10)); //20
System.out.println(plus10.compose(multiply2).apply(1)); // (1 * 2) + 10 = 12
System.out.println(plus10.andThen(multiply2).apply(1)); // (1 + 10) * 2 = 22

Function에서 사용하는 결과 반환용 'apply'와 조합용 함수 'compose', 'andThen' 모두 사용 가능합니다.

 

Function과의 차이점은 Function은 입력과 출력 type이 다를 수 있지만

UnaryOperator는 입력과 출력 type이 동일해야 합니다.

 

 

 


 

 

BinaryOperator

 

BinaryOperator<T>

BiFunction<T, U, R>의 특수한 형태로, 동일한 타입의 입렵값 두개를 받아 리턴하는 함수 인터페이스 입니다.

입력 type과 반환 type이 같습니다.

 

// 두 정수를 더하기
BinaryOperator<Integer> add = (a, b) -> a + b;
System.out.println(add.apply(5, 3)); // 8

// 두 문자열 결합
BinaryOperator<String> concatenate = (str1, str2) -> str1 + str2;
System.out.println(concatenate.apply("Hello, ", "World!")); // "Hello, World!"

 

BiFunction 과의 차이점은 BiFunction 은 입력과 출력 type이 다를 수 있지만

BinaryOperator는 입력과 출력 type이 동일해야 합니다.

 

 

 

 

 

 

 

https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html

 

java.util.function (Java Platform SE 8 )

Interface Summary  Interface Description BiConsumer Represents an operation that accepts two input arguments and returns no result. BiFunction Represents a function that accepts two arguments and produces a result. BinaryOperator Represents an operation u

docs.oracle.com

위 사이트에서 더 다양한 함수 인터페이스의 정의를 확인 할 수 있습니다.