Java Funtional Interface

Hyun Jin Kim·2021년 8월 1일
0

Java

목록 보기
5/7

#1 Funtional Interface?

1-1 함수형 인터페이스란?

  • 1개의 추상 메서드를 갖는 인터페이스
    - default method 또는 static method는 여러 개를 가져도 상관 없다.
    • abstract 키워드는 생략 가능하다.
  • @FuntionalInterface
    - 함수형 인터페이스의 조건을 검사해준다
    - 추상 메서드가 1개를 초과하는 등 조건을 어겼을 때 오류를 반환

1-2 함수형 인터페이스 만들기

@FunctionalInterface
public interface FuncInterface {
	// 추상메서드가 1개 존재
	void abstMethod(); // abastract 키워드는 생략가능
}

#2 Why?

2-1 Lambda

  • 함수형 인터페이스로만 람다식은 사용가능하다.

#3 기본 함수형 인터페이스

3-1 Predicate

  • 값을 받아서 boolean 타입을 반환
  • filter와 주로 쓰임
    - filter의 조건을 통해 값의 true/false 여부를 판단
public class Func {

    public static void main(String[] args) {

        List<String> cities = Arrays.asList("Seoul", "Tokyo", "New York", "Frankfurt", "Shanghai");

        List<String> cityWithS = cities.stream() // stream 생성
                .filter(city -> city.startsWith("S")) // filter를 통해 S로 시작하는 문자열 걸러내기
                // Stream<T> filter(Predicate<? super T> predicate);

                .collect(Collectors.toList());
        System.out.println(cityWithS);
    }
}
    }
}

3-2 Consumer

3-3 Supplier

3-4 Function<T,R>

3-5 Comparator

3-6 Runnable

3-7 Callabel

profile
우아하고 싶어서 만든 블로그

0개의 댓글