Search code examples
javaenumscomparablecompareto

Why is compareTo on an Enum final in Java?


An enum in Java implements the Comparable interface. It would have been nice to override Comparable's compareTo method, but here it's marked as final. The default natural order on Enum's compareTo is the listed order.

Does anyone know why a Java enums have this restriction?


Solution

  • For consistency I guess... when you see an enum type, you know for a fact that its natural ordering is the order in which the constants are declared.

    To workaround this, you can easily create your own Comparator<MyEnum> and use it whenever you need a different ordering:

    enum MyEnum
    {
        DOG("woof"),
        CAT("meow");
    
        String sound;    
        MyEnum(String s) { sound = s; }
    }
    
    class MyEnumComparator implements Comparator<MyEnum>
    {
        public int compare(MyEnum o1, MyEnum o2)
        {
            return -o1.compareTo(o2); // this flips the order
            return o1.sound.length() - o2.sound.length(); // this compares length
        }
    }
    

    You can use the Comparator directly:

    MyEnumComparator comparator = new MyEnumComparator();
    int order = comparator.compare(MyEnum.CAT, MyEnum.DOG);
    

    or use it in collections or arrays:

    NavigableSet<MyEnum> set = new TreeSet<MyEnum>(comparator);
    MyEnum[] array = MyEnum.values();
    Arrays.sort(array, comparator);    
    

    Further information: