본문 바로가기
언어

[Java] Java8 - 함수형 프로그래밍 (3)

by 코린이 프로도 2022. 10. 22.
반응형

Java 8 시리지는 인프런 강의를 듣고 정리한 내용이다.

참고 : https://www.inflearn.com/course/the-java-java8/dashboard

 

더 자바, Java 8 - 인프런 | 강의

자바 8에 추가된 기능들은 자바가 제공하는 API는 물론이고 스프링 같은 제 3의 라이브러리 및 프레임워크에서도 널리 사용되고 있습니다. 이 시대의 자바 개발자라면 반드시 알아야 합니다. 이

www.inflearn.com

자바에서 제공하는 함수형 인터페이스


 따로 함수형 인터페이스를 만들지 않아도 자바에서 제공하는 함수형 인터페이스를 가져다가 사용해도 된다. 

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

자주 사용하는 자바 함수형 인터페이스를 알아보자

목록

  • Function<T, R>
  • BiFunction<T, U, R>
  • Consumer<T>
  • Supplier<T>
  • Predicate<T>
  • UnaryOperator<T>
  • BinaryOperator<T>

Function<T, R>

T 자료형의 값을 받아서 R 자료형으로 리턴하는 함수 인터페이스.

R apply(T t) 메소드를 정의하여 사용하면 된다.

 

소스 코드

public class Foo {
	public static void main(String[] args) {
    	Function<Integer, String> message = (num) -> Integer.parseInt(num) + " hello test";
        Function<Integer, Integer> plus = (i) -> i + 10;
    }
}

또 이 Function 함수형 인터페이스로 구현한 메소드를 조합하는 것도 가능하다. andThen, compose

public class Foo {
	public static void main(String[] args) {
    	Function<Integer, String> message = (num) -> num + " hello test";
        Function<Integer, Integer> plus = (i) -> i + 10;
        
        System.out.println(plus.andThen(message).apply(1)); // 출력값 : 11 hello test
        System.out.println(message.compose(plus).apply(1)); // 출력값 : 11 hello test
    }
}

BiFunction<T, U, R>

두 개의 입력값(T, U)를 받아 R을 리턴하는 함수형 인터페이스

R apply(T t, U u) 

 

소스 코드 

public class Foo {
	public static void main(String[] args) {
    	BiFunction<Integer, Integer, Integer> plus = (first, second) -> first + second;
        System.out.println("두 수의 합 : " + plus.apply(1,2)); //출력 값 : "두 수의 합 : 3"
    }
}

Consumer<T>

T 타입 하나를 받아서 아무 값도 리턴하지 않는 함수 인터페이스 

void Accept(T t)

함수 조합 가능하다. andThen

 

소스 코드 

public class Foo {
	public static void main(String[] args) {
    	Consumer<String> printMessage = (msg) -> System.out.println(msg);
        printMessage.accept("Functional Interface test (Consumer<T>)"); 
    }
}

Supplier<T>

T 타입 값을 제공하는 함수 인터페이스. 입력 값이 없고 원하는 값을 세팅해 반환해 준다.

T get() 

 

소스 코드 

public class Foo {
	public static void main(String[] args) {
    	Supplier<String> supplier= () -> "test supplier";
        System.out.println(supplier.get()); // test supplier 출력
    }
}

Predicate<T>

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

boolean test(T t) 

함수 조합용 메소드로 And, Or, Negate가 있다.

 

소스 코드 

public class Foo {
	public static void main(String[] args) {
    	Predicate<Integer> predicate = (num) -> num>10;
        System.out.println(predicate.test(2)); // 출력 값 : fasle
    }
}

UnaryOperator<T>

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

 

소스 코드 

public class Foo {
	public static void main(String[] args) {
    	UnaryOperator<Integer> multiply = (num) -> num*2;
        System.out.println(multiply.apply(5));  // 10 출력
    }
}

BinaryOperator<T>

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

 

소스 코드 

public class Foo {
	public static void main(String[] args) {
    	BinaryOperator<Integer> multiply = (num, num2) -> num * num2;
        System.out.println(multiply.apply(5,10));  // 50 출력
    }
}
반응형