ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 메서드 레퍼런스란
    개발/JAVA 2021. 8. 14. 15:18

    메서드 레퍼런스

    • 우리말로 번역하면 메서드 참조라는 의미
    • 람다 표현식 body(몸체) 부분에 기술되는 메서드를 이용해서 표현되며, 메서드의 이름만 전달한다.
    • 구분자(::)를 붙이는 방식으로 메서드 레퍼런스를 표현한다.
    • 메서드 레퍼런스를 사용하면 람다 표현식이 더욱 간결해진다.

    메서드 레퍼런스 표현 예)

    (Car car) -> car.getCarName() = Car::getCarName

     

    메서드 레퍼런서의 유형

    • ClassName::static method
      • 예) (String s) → Integer.parseint(s) 메서드 레퍼런스로 표현하면 Integer::parseeint
    • ClassName::instance method
      • (String s) → s.toLowerCase() 메서드 레퍼런스로 표현 하면 String::toLowerCase
    • object::instance method
      • (int count) → obj.getTotal(count) 메서드 레퍼런스로 표현하면 obj::getTotal
    • ClassName::New
      • () → new Car() 변환 Car::new

     

    메서드 레퍼런스 예제

    • Predicate p = s → s.isEmpty() ⇒ Predicate p = String::isEmpty
    • Funtion<Integer, String> f = i → String.valueOf(i) ⇒ Funtion<Integer, String> f = String::valueOf
    • BiPredicate<List, Integer> p = (list, num) → list.contains(num)
    • ⇒ BiPredicate<List<Integer>, Integer> p = List::contains
    • Consumer c = s → System.out.println(s) ⇒ Consumer c = System.out::println
    • BiFuntion<String, Car, Car> d = (s1, s2) → new Car(s1, s2)
    • ⇒ BiFuntion<String, Car, Car> d = Car::new

    '개발 > JAVA' 카테고리의 다른 글

    JAVA 커맨드 라인에서 컴파일 하기  (0) 2020.12.27

    댓글

Designed by Tistory.