• Post Reply Bookmark Topic Watch Topic
  • New Topic

Enumerations in java world ( jdk 1.5)  RSS feed

 
Ramanjulu Balamurugan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We need to use Enumerations in our project. We have a requirement that we need to check the values in enumeration directly in a single line without iterating everytime. Is there any way that we can go for it... Its like CheckingVariable.equals(enumerationObject.theKnownValue) ... I am sure i dont want to iterate and check everytime in a method.
 
Mark Spritzler
ranger
Sheriff
Posts: 17309
11
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am going to move this to the Java In General forum. This forum is for JSF Questions.

Thanks

Mark
 
Garrett Rowe
Ranch Hand
Posts: 1296
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure I understand the question, I'm not sure if you are talking about a java.util.Enumeration or an Enum type. If its a java.util.Enumeration then there not a way to access an element of the Enumeration without iterating through all the elements that come before it in the Enumeration. If its an Enum type, you could consider using a HashSet or the possibly even faster EnumSet.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ramanjulu Balamurugan:
...Is there any way that we can go for it... Its like CheckingVariable.equals(enumerationObject.theKnownValue) ...

Welcome to JavaRanch!

It's more like...

checkingVariable == MyEnums.THIS_ENUM.ordinal();

See Enums and also check the API documentation for the Enum class (where the ordinal method is defined).
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmmm... I just assumed this meant the Enum type. I didn't even consider the Enumeration interface until I saw Garrett's post. Yes, the Enumeration interface is like an old version of Iterator. It iterates.
 
Ramanjulu Balamurugan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys -- yes you are right though We have good methods in iterators but iam wondering why our architects still go for Enumerations -- BTW thanks for your kind reply ...
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, so you are talking about interface java.util.Enumeration (and not about enums).

Enumeration has been replaced by Iterator since the new collections framework was added in Java 1.2. There are still places in the standard Java API where Enumeration is used, but there is no reason to use Enumeration instead of Iterator in new Java code.

So, you should ask your architects why they still use Enumeration instead of Iterator.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!