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
Post a Comment