Alex Rivera | Logout

Is there any reason EnumMap and EnumSet are not Navigable

Asked 2012-07-17T11:18:08.163
36

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?

Edit
Report

1 Answer

2

The post doesn't directly answers the question, neither attempts to, it merely conveys why Navigable was introduced

Post as I was requested to (and it's too long for a comment)

The short answer is that Navigable exists because we didn't have anything like upcoming "defenders" -- Sorted didn't describe all the common functionality, and there was no way to do so except to introduce a new interface. In practice, I'm sure "Sorted" is still used much more often than "Navigable" as a declaration type, because most people don't need the methods defined in Navigable but not Sorted. Plus "Navigable" is just not a very nice name :-)

-Doug

answered 2012-07-24T09:46:18.847

Your Answer