Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
412 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:

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 :-)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...