Search code examples
javacollectionsenums

Is there any reason EnumMap and EnumSet are not Navigable


Enum is Comparable which means you can have

NavigableSet<AccessMode> modes = new TreeSet<>();
NavigableMap<AccessMode, Object> modeMap = new TreeMap<>();

These have O(ln N) access times.

The Enum collections have O(1) access times, but are not Navigable

NavigableSet<AccessMode> modes = EnumSet.noneOf(AccessMode.class); // doesn't compile
NavigableMap<AccessMode, Object> modeMap = new EnumMap<>(AccessMode.class);  // doesn't compile

I was wondering if there was a reason Enum collections were not Navigable (and Sorted). i.e Am I missing something?


Solution

  • My best guess is that navigability was not seen as a major use case for enum sets. There is nothing in the implementation that would prevent navigability. The rare use cases that combine the need for a set of enum members with navigability are covered by the TreeSet and TreeMap.