• Post Reply Bookmark Topic Watch Topic
  • New Topic

EnumSet  RSS feed

 
John Rask
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've come across a situation where an EnumSet would probably be most appropriate. However, after reading the javadocs and perusing the source code for EnumSet, I find myself wondering about its specifications. Am I correct that EnumSet is not like EnumMap in the sense that HashSet is like HashMap - namely, that the HashSet is backed by a HashMap but that this isn't the case for the EnumSet/Map? And, does EnumSet.contains() run in O(n) time by iterating through all the elements of an internal array rather than by looking up in an array with the enum ordinal like EnumMap does? If so, then should I consider using an EnumMap<SomeEnumClass,Object> instead of an EnumSet?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If it isn't using an EnumMap behind the scenes, it doesn't mean it isn't using an equivalent approach. If you check the docs for java.util.EnumSet, it says:
...Enum sets are represented internally as bit vectors. ...

Implementation note: All basic operations execute in constant time. They are likely (though not guaranteed) to be much faster than their HashSet counterparts. Even bulk operations execute in constant time if their argument is also an enum set.

So if an EnumSet fits your problem, don't be afraid to use it.
 
Campbell Ritchie
Marshal
Posts: 56529
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there anything about enum set in the Java Tutorials?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!