JavaBookStudy / JavaBook

책읽기 스터디
https://javabookstudy.github.io/
Apache License 2.0
19 stars 2 forks source link

[Effective Java] Item 34. 코드 34-7에 콜론 두개 #24

Closed kjsu0209 closed 3 years ago

kjsu0209 commented 3 years ago
private static final Map<String, Operation> stringToEnum =
    Stream.of(values()).collect(
        toMap(Object::toString, e -> e));

216p 코드 보면 toMap 메서드 인자로 Object::toString이 있는데 무슨 의미일까요?? 🤔

taxol1203 commented 3 years ago

이중 콜론 연산자(::)를 통해서 Object의 toString을 호출하여, values가 반환한 배열의 문자열을 맵에 추가하는 것이 아닐까 추측을 해봅니다!

http://wiki.sys4u.co.kr/pages/viewpage.action?pageId=7766725

daebalprime commented 3 years ago

http://yoonbumtae.com/?p=2776 첫 예제를 참조하시기 바랍니다.

forEach의 첫 번째 구문은 람다식이 x를 파라미터로 넘기고 println(x)이 그 파라미터를 받는 과정에서 x를 두 번 사용하게 됩니다. 람다식이 건네는 파라미터와 받는 쪽의 파라미터가 동일할 때, 두 번째 구문처럼 System.out::println으로 줄여쓸 수 있습니다.