Enum singleton versus non-capturing lambda
Brian Goetz
brian.goetz at oracle.com
Wed Mar 13 17:09:36 PDT 2013
Free, compact, guaranteed-stable serialization format.
On 3/13/2013 8:03 PM, Zhong Yu wrote:
> On Wed, Mar 13, 2013 at 4:51 PM, Michael Hixson
> <michael.hixson at gmail.com> wrote:
>> I see a couple of different forms for returning functional interface
>> instances from static methods in the current lambda code.
>>
>> 1. Enum singleton, as in Comparators.naturalOrder()
>>
>> private enum NaturalOrderComparator implements
>> Comparator<Comparable<Object>> {
>> INSTANCE;
>> public int compare(Comparable<Object> c1, Comparable<Object> c2) {
>> return c1.compareTo(c2);
>> }
>> }
>>
>> public static <T extends Comparable<? super T>> Comparator<T> naturalOrder() {
>> return (Comparator<T>) NaturalOrderComparator.INSTANCE;
>> }
>>
>> 2. Non-capturing lambda, as in Functions.identity()
>>
>> public static <T> Function<T, T> identity() {
>> return t -> t;
>> }
>>
>> Code complexity aside, does either approach have advantages over the
>> other? Does one perform better/worse or serialize better/worse?
>
> I'd like to know too. What's the purpose of enum in the 1st example?
>
>>
>> -Michael
>>
>
More information about the lambda-dev
mailing list