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

java - SSE Emitter : Manage timeouts and complete() -

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

java - How to resolve error - package com.squareup.okhttp3 doesn't exist? -