• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Bidirectional mapping

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
With a Hashmap you can get a value for a given key. But you can't get a key that belongs to a (unique) value.

Does such a class exist? E.g. if you want to associate an English name with its French translation.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
As far as I know such a class does not exist, because only key is to be considered unique and values could be duplicated.

May be you have to make up your own method to do so.

Could you please let me know what exactly is your requirement?
 
Sheriff
Posts: 22656
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Apache Commons Collections has some interfaces and classes for this.
 
Jan Sterk
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your replies.

Originally posted by Jade Jadaaih:
...
Could you please let me know what exactly is your requirement?



While I was trying for an hour or so to write down what the exact problem was, I found the solution
 
Jade Jadaaih
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you please let me know your solution? could be helpful
 
Jan Sterk
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The solution is to not use one side of the mapping

The program processes an xml file, which contains nodes with a 'type' attribute. One part of the program (the model) passes these types to another part (the view).

To keep things typesafe, I created an enum for the known types. This is were the mapping comes in. Each type from the xml (a String) maps to an enum.

The program reads the types, stores them in a List, and at a later point creates new nodes with those types.

My first approach was to store the enum that belongs to the type xml-String, and store that enum in the List. But then when a new node has to be created, the enum has to be translated back to the xml-String. That's where I thought a bi-directional mapping was needed.

One solution is not to store the enums, but the xml-Strings as they're read from the xml. When they have to be passed to the view-part, they can be translated to the enums at that moment. It has another advantage - it seperates the type that is used for reading/writing the xml, from the type that is used by the view, which is a good thing. So actually, the String-enum mapping is not one-to-one: several Strings can potentially map to the same enum.

Now that I think of it, I may be able to get rid of the xmlString-to-enum hashmap. Maybe I can store the String in the enum class.. That would be better OO - the String and enum relationship is then encapsulated in one class. I'm not an enum-expert, I'll have to check it out.
[ October 20, 2008: Message edited by: Jan Sterk ]
 
Jade Jadaaih
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks...It did help.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic