Enum singleton versus non-capturing lambda
Zhong Yu
zhong.j.yu at gmail.com
Wed Mar 13 17:51:09 PDT 2013
On Wed, Mar 13, 2013 at 7:09 PM, Brian Goetz <brian.goetz at oracle.com> wrote:
> Free, compact, guaranteed-stable serialization format.
It seems that the serialized format of the enum singleton is 40 bytes
longer than a traditional singleton, if that's a concern to anyone.
>
>
> 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