Enum singleton versus non-capturing lambda

Michael Hixson michael.hixson at gmail.com
Wed Mar 13 14:51:26 PDT 2013

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>> {
    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?


More information about the lambda-dev mailing list