• Post Reply Bookmark Topic Watch Topic
  • New Topic

Use of enum  RSS feed

 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi. here is my Enum



when saving value in database i am storing return value
When i want to retrieve record i have return value i.e 0 or 1..Now to get the name
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why 0 or 1? Use the name as a string and then you can use the valueOf() method.
 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the enum was previously written in the company cant change that.

O and 1 is used to store it in database ,it is value of dropdown.and key is enum value

Key value
BOOKING 0
CANCELLATION 1
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all I would change the enums to make the 0/1 part a property of the enum:

You'll now need a mapping from 0/1 to the enum values. You can do this in a few ways:
- loop through the enum constants, check if the value matches the current constant's value. If so, return that constant. If no constant matches throw an IllegalArgumentException. This will make it behave like valueOf(String)

- keep a Map<Integer,DealType> that you use for direct lookup. Because the Map must be declared and initialized after the enum constants, use a static initializer block:
Afterwards you can do a simple int/Integer to DealType lookup, throwing an IllegalArgumentException if the value has no matching type.

Now, which of these two techniques is better cannot be answered. One is faster but requires a bit more memory (the Map), the other requires no extra memory but is slightly slower (the loop).
 
Satyajit Bhadange
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Very Much...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!