WW. WW. - 1 year ago 49
Java Question

Is there a succinct way to create a Comparator<X> based on ordering by X.getY()

Is there any shorter way to say this:

final Comparator<ClassA> byName =
(final ClassA a1, final ClassA a2)
-> a1.getName().compareTo( a2.getName() ));

I know that getName() will never return null.

Perhaps something along these lines using a method reference:

final Comparator<ClassA> byName = ????( ClassA::getName );

Answer Source

you can shorten it to:

final Comparator<ClassA> byName =
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download