Vincenzo Cosi Vincenzo Cosi - 1 month ago 7
Java Question

Java 8: parameters in method reference

Is possible to pass parametres using method reference?
For example, I have to create a

TreeMap
but using
reverseOrder()
.
Does something like
TreeMap::new(reverseOrder())
exist?

Answer Source

No, you can't do it with a method reference.

You can use lambda expressions instead:

() -> new TreeMap<TheRelevantType>(reverseOrder())

or

() -> new TreeMap<>(reverseOrder())

if you are using this expression in a context where the compiler can infer the element type of the TreeMap.