In certain cases when I use a Stream on a list of Objects, I like to collect them via the Collectors.toMap function and assign an important attribute as the key and the object itself as the value, like in this case:
Map<String, SampleObject> map = list.stream()
.collect(Collectors.toMap(SampleObject::getImportantValue, v -> v));
Usually I use the double colon operator to assign the key, but for the value I resort to the v -> v
construct.
This made me wonder:
Is there a way to assign the object itself as the return value of the anonymous function by using double colons? From my own testing it appears that SampleObject
and SampleObject::
don't work. (Only logical, as the former only references the class and the latter expects a method to follow)
As a naive approach I would expect something functionally similar to this:
...collect(Collectors.toMap(SampleObject::getImportantValue, SampleObject::));