Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 479
Peter Lawrey Avatar asked Jul 17 '12 11:07

Peter Lawrey


People also ask

Is EnumSet immutable?

Yes EnumSet is modifiable.

What is EnumSet and EnumMap in Java?

EnumMap is a specialized implementation of the Map interface for the enumeration types. EnumSet is a specialized implementation of the Set interface for the enumeration types. EnumMap is internally represented as an array. EnumSet is internally represented as a BitVector.

Is EnumSet ordered?

By default Enums declaration order defines the natural ordering of the Enums. From javadoc of compareTo: Enum constants are only comparable to other enum constants of the same enum type. The natural order implemented by this method is the order in which the constants are declared.


2 Answers

Many "obvious" features are missing from the JDK and its various APIs. Why this particular feature was omitted / forgotten? We can only guess. But your question has been a RFE at Sun/Oracle for a long time:

  • http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6278287
  • http://www.java.net/node/644910

You could support those RFEs by commenting on them. Note, here's an authoritative answer by Joshua Bloch on the subject:

I vaguely recall considering it, but I can't recall whether we explicitly rejected it with good reason. We were running very low on time when I implemented EnumSet and EnumMap, and it's possible that time played a role in our decision

http://comments.gmane.org/gmane.comp.java.jsr.166-concurrency/2158

So even he had to guess :-)

like image 174
Lukas Eder Avatar answered Sep 22 '22 19:09

Lukas Eder


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.

like image 41
Marko Topolnik Avatar answered Sep 19 '22 19:09

Marko Topolnik