Win a copy of Emmy in the Key of Code this week in the General Computing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

EnumSet class features

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have few queries related to enum and EnumSet:-

1. When to use EnumSet, any specific example which shows EnumSet is required.
2. Whats the meaning that Enum Constants are not compiled into clients so you can freely add, remove, or reorder them without recompiling the clients? Hoiw is it possible to add new values without compiling the code.
3 Can someone please explain this:- Enum sets are represented internally as bit vectors.

Thanks in Advance!!
 
Marshal
Posts: 66135
250
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How much do you know about EnumSet already?
 
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rajat Jindal wrote:1. When to use EnumSet, any specific example which shows EnumSet is required.


I don't think it's ever required, since enums are objects, just like any other; it's certainly advisable when the List elements are all enums though.

2. Whats the meaning that Enum Constants are not compiled into clients so you can freely add, remove, or reorder them without recompiling the clients? Hoiw is it possible to add new values without compiling the code.


It isn't. However, re-compiling the enum is NOT the same as re-compiling the client (which might be an entire application).

3 Can someone please explain this:- Enum sets are represented internally as bit vectors.


It's simply describing a form of optimization - and IMO, it would have been MUCH better if they'd left it out.
What it's basically saying is that EnumSets are blisteringly fast; however, it's not something you (as a client) should be worried about.

HIH

Winston
 
Rajat Jindal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Winston Gutkowski and Campbell Ritchie !!

Actually I am reading Thinking in Java 4th Edition and it is mentioned :-

EnumSets are built on top of longs, a long is 64 bits, and each enum instance requires one bit to indicate presence or absence. This means you can have an EnumSet for an enum of up to 64 elements without going beyond the use of a single long.



/* Output:
[A0, A1, A2, A3, A4, A5, A6, A7, A8, A9, A10, A11, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21, A22, A23, A24, A25, A26, A27, A28, A29, A30, A31, A32, A33, A34, A35, A36, A37, A38, A39, A40, A41, A42, A43, A44, A45, A46, A47, A48, A49, A50, A51, A52, A53, A54, A55, A56, A57, A58, A59, A60, A61, A62, A63, A64, A65, A66, A67, A68, A69, A70, A71, A72, A73, A74, A75]
*///:~
The EnumSet clearly has no problem with an enum that has more than 64 elements, so we may presume that it adds another long when necessary.

Queries:-
1. Can someone please explain about these 64 bits concept here. Still more than 64 elements can be represented... ???
2. EnumSets are used in place of flags. That statement is also not very clear to me.
 
Winston Gutkowski
Bartender
Posts: 10777
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rajat Jindal wrote:The EnumSet clearly has no problem with an enum that has more than 64 elements, so we may presume that it adds another long when necessary.


No you can't. All you can "presume" is that it handles more than 64 elements. Indeed it might return a completely different type of List for such a case; the designers were just smart enough not to tell you, because it's NOT something you need to know.

1. Can someone please explain about these 64 bits concept here. Still more than 64 elements can be represented... ???


Nope. Not unless it's detailed in the documentation (and there's no reason why it should be).
My advice: DontSweatIt.

2. EnumSets are used in place of flags. That statement is also not very clear to me.


I think what they mean is that EnumSets are used as an alternative to numbers that use to be used in older languages (and indeed, earlier releases of Java) to denote a set of 'bit' flags that were either "ON" or "OFF" (or "PRESENT" and "ABSENT").

For example, if you had three things that could be either "ON" or "OFF", you could hold all combinations of them in a number between 0 (all "OFF") and 7 (all "ON"). And Java has all sorts of 'bitwise' operators that allow you to "set" or "clear" specific bits in a number - and they're extremely fast.

HIH

Winston
 
Sheriff
Posts: 21817
104
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're interested in how EnumSet works, in your JDK folder there is a file called src.zip that includes the source code. For EnumSet, there are actually three classes: EnumSet itself, RegularEnumSet (package private, for enums with <=64 constants) and JumboEnumSet (package private, for enums with > 64 constants).
 
Stinging nettles are edible. But I really want to see you try to eat this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!