추상메서드가 1개만 정의된 인터페이스
두 개 이상의 메서드 선언 시 컴파일 오류가 발생
@FunctionalInterface
public interface Car {
public void move();
}
역할: 하나의 인자를 받아 특정 작업을 수행하지만, 결과값을 반환하지 않는다.
주요 메서드 : void accept(T t)
Consumer<String> consumer3 = s -> System.out.println("출력문 = " + s);
consumer3.accept("하이하이!");
출력문 = 하이하이!
역할 : 인자를 받지 않고, 결과값을 제공하는 역할을 한다. 즉, 필요할 때 값을 "공급"해주는 역할
주요 메서드 : T get()
Supplier<Integer> supplier = () -> (int) (Math.random() * 100);
Integer ramdomValue = supplier.get();
System.out.println("ramdomValue = " + ramdomValue);
ramdomValue = 랜덤값
역할: 하나의 인자 T를 받아서 결과값 R을 반환
주요 메서드: R apply(T t)
String helloWorld = "Hi Hello";
Function<String, String> convertUpper2 = s -> s.toUpperCase();
String applied = convertUpper2.apply(helloWorld);
System.out.println("applied = " + applied);
Function<String, Integer> getSize = s -> s.length();
Integer size = getSize.apply(helloWorld);
System.out.println("size = " + size);
applied = HI HELLO
size = 8