본문 바로가기
Programming/Java

[자바/Java] 함수형 인터페이스

by 코딩하는 랄로 2023. 10. 14.
728x90

함수형 인터페이스

이전 글인 lambda에서 함수형 인터페이스에 대해서 알아보았다. 하지만 매번 함수형 인터페이스를 직접 만들어서 사용하는 것을 번거로운 일이다.

 

그래서 Java에서는 기본적으로 많이 사용되는 함수형 인터페이스를 제공하고 있다. 기본적으로 제공되는 것만 사용하여도 대부분의 람다식을 사용할 수 있다.

 

함수형 인터페이스의 종류는 아래와 같다.

 

  • Runnable :  () -> void
  • Supplier<T> : () -> T
  • Consumer<T> : T -> void
  • Function<T, R> : T -> R
  • Predicate<T> : T -> boolean
  • UnaryOperator<T> : T -> T
  • BinaryOperator<T> : (T, T) -> T
  • BiPredicate<T, U> : (T, U) -> boolean
  • BiConsumer<T, U> : (T, U) -> void
  • BiFuction<T, U, R> : (T, U) -> R
  • Comparator<T> : (T, T) -> int

 

 

Runnable

기존부터 존재하던 인터페이스로 메소드명은 run이고 매개변수는 없고 리턴타입은 void이다. 

@FunctionalInterface
public interface Runnalbe {
    public abstract void run();
}

public class Main {
    public static void main(String[] args) {
    
        Runnable r = () -> System.out.println("Hello");
        r.run();
        
    }
}

 

 

Supplier<T>

매개변수가 없고 리턴 타입은 T인 get 메소드를 가지고 있다.

@FunctionalInterface
public interface Supplier<T> {
    T get();
}

public class Main {
    public static void main(String[] args) {
    
        Supplier<String> s = () -> "Hello";
        String result = s.get();
        System.out.println(result);
        
    }
}

 

 

Consumer<T>

매개변수는 하나를 가지고, 리턴 타입은 void인 메소드 accept를 가지고 있다.

@FunctionalInterface
public interface Consumer<T> {
    void accept(T t);
}

public class Main {
    public static void main(String[] args) {
    
        Consumer<String> c = str -> System.out.println(str);
        c.accept("Hello");
        
    }
}

 

 

Function<T, R>

하나의 매개변수 T를 가지고, 리턴 타입은 R인 메소드 apply 메소드를 가지고 있다.

@FunctionalInterface
public interface Function<T, R> {
    R apply(T t);
}

public class Main {
    public static void main(String[] args) {
    
        Function<String, Integer> f = str -> Integer.parseInt(str);
        Integer n1 = f.apply("1");
        System.out.println(n1);
        
    }
}

 

 

Predicate<T>

하나의 매개변수 T를 가지고 리턴 타입은 boolean인 메소드 test를 가지고 있다.

@FunctionalInterface
public interface Predicate<T> {
    boolean test(T t);
}

public class Main {
    public static void main(String[] args) {
    
        Predicate<String> p = str -> str.isEmpty();
        boolean result = p.test("hello");
        System.out.println(result);   // false
        System.out.println(p.test("")); //true
        
    }
}

 

 

UnaryOperator<T>

하나의 매개변수 T를 가지고, 리턴타입도 T인 메소드 apply를 가지고 있다.

@FunctionalInterface
public interface UnaryOperator<T> {
    T apply(T t);
}

public class Main {
    public static void main(String[] args) {
    
        UnaryOperator<String> u = str -> str + " operator";
        String result = u.apply("hello unary");
        System.out.println(result);
        
    }
}

 

 

BinaryOperater<T>

동일 타입(T) 매개변수 2개를 가지고 리턴 타입도 같은 타입인 T를 가지는 메소드 apply를 가지고 있다.

@FunctionalInterface
public interface BinaryOperator<T> {
    T apply(T t1, T t2);
}

public class Main {
    public static void main(String[] args) {
    
        BinaryOperator<String> b = (str1, str2) -> str1 + " " + str2;
        String result = b.apply("Hello", "BinaryOperator");
        System.out.println(result);
        
    }
}

 

 

BiPredicate<T, U>

서로 다른 타입인 매개변수 T, U 두개를 가지고 리턴 타입은 boolean인 메소드 test를 가진다.

@FunctionalInterface
public interface BiPredicate<T, U> {
    boolean test(T t, U u);
}

public class Main {
    public static void main(String[] args) {
    
        BiPredicate<String, Integer> bp = (str, num) -> str.equals(Integer.toString(num));
        boolean result = bp.test("1", 1);
        System.out.println(result);
        
    }
}

 

 

BiConsumer<T, U>

서로 다른 타입인 매개변수 T, U를 가지고 리턴 타입은 void인 메소드 accept를 가진다.

@FunctionalInterface
public interface BiConsumer<T, U> {
    void accept(T t, U u);
}

public class Main {
    public static void main(String[] args) {
    
        BiConsumer<String, Integer> bc = (str, num) -> System.out.println(str + " :: " + num);
        bc.accept("숫자", 5);
        
    }
}

 

 

BiFunction<T, U, R>

서로 다른 타입인 매개변수 T, U를 가지고 리턴 타입은 R인 메소드 apply를 가진다.

@FunctionalInterface
public interface BiFunction<T, U, R> {
    R apply(T t, U u);
}

public class Main {
    public static void main(String[] args) {
    
        BiFunction<Integer, String, String> bf = (num, str) -> String.valueOf(num) + str;
        String result = bf.apply(5, "678");
        System.out.println(result);
        
    }
}

 

 

Comparator<T>

자바의 전통적인 함수형 인터페이스 중 하나로 객체의 우선순위를 비교한다. 동일 타입의 매개변수 T 2개를 가지고 리턴 타입은 정수인 compare 메소드를 가지고 있다.

@FunctionalInterface
public interface Comparator<T> {
    int compare(T t1, T t2);
}

public class Main {
    public static void main(String[] args) {
    
        Comparator<String> c = (str1, str2) -> str1.compareTo(str2);
        int result = c.compare("aaa", "bbb");
        System.out.println(result);
        
    }
}
728x90