lambda - Java Method reference not expected here -


how chain method references instances java 8? example:

collections.sort(civs,comparator.comparing(civilization::getstrategy.getstrateglevel)); 

getstrategy of civilization instance returns strategy object instance has instance method getstrategylevel.

why doesn't comparator.comparing method return comparator it's functional interface implemented lambda expression?

in case, should use lambda, can't apply method reference directly:

collections.sort(civs, collectors.comparing(c -> c.getstrategy().getstrateglevel())); 

though, there way use method reference here. assuming have class like

class civilizationutils {     public static integer getkeyextractor(civilization c) {         return c.getstrategy().getstrateglevel();     } } 

the issue solved like

collections.sort(civs, collectors.comparing(civilizationutils::getkeyextractor)); 

Comments

Popular posts from this blog

jquery - uncaught exception: DataTables Editor - remote hosting of code not allowed -

python - multiprocessing pool with map -

python - Poor performance of db. inserts using django-mssql -