Help coderanch get a
new server
by contributing to the fundraiser
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Map 2 enums

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have 2 enums like below





How can i map the same names in 2 enums and get message from enum 1 and code from enum 2.
 
author & internet detective
Posts: 41937
911
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vani,
This feels like poor design. Why not just merge the two enums into one that has all the info you need? Or create another class that actually maps one enum (as key) to the other (as value)?

It is possible to do what you are asking, just brittle. You can convert the first enum to a String and then use that String to create the right second enum:


Also, note that it is convention to begin class/enum names with an uppercase letter.
 
Greenhorn
Posts: 12
Mac Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vani Kumar wrote:I have 2 enums like below



Woah! name() is part of the base API for enums - it returns TEST_ENUM_1 or TEST_ENUM_2 in this case. It's a bad idea to hide or override that method by declaring anything called 'name' in the enum class.

However, in answer to your question you are probably after the static method 'valueOf' on java.lang.Enum. This will be ok if you want to use the java language name() to get from Enum1 to Enum2. If you want to use your special name feild, then you can use EnumSet to go through all the enums in enum2 and find the one you are looking for, or you can use the constructor in enum2 to build a Map.

Go look at the API docs for java.lang.Enum.name() and .valueOf(), and look at the API docs for EnumSet.
 
Evil is afoot. But this tiny ad is just an ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic