This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

elegant way to reverse key/value in Map  RSS feed

 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a Map<String,String> (actually a TreeMap) that I need to reverse the keys and values because I need the keys corresponding to the values in alphabetical order.

Any elegant way to do this other than iterating the keys and adding the swapped key/value pair to a new Map?

Thanks,
 
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What characteristics are you looking for? If this is a one-time operation that can be slow, iterating the keys and creating a new map is fine, I'm not sure there is a more elegant way (it's a few lines of code, regardless). Otherwise, if you need the reverse map fast, you might consider creating a "DualMap" class that encapsulates the primary map and maintains a secondary map as new keys are added.

-Jeff-
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeff,

It's a parameter list from a web page. The parameter names are userIDs in a database, and the parameter values are web-user assigned rankings. I need the IDs in user ranked order. I saw two ways to do it. Get the ParameterNames enumeration, iterate it, and create a sorted map. Or I can get the parameter key/value pairs as a Map, then get the keys as a collection, iterate them, and create a new sorted map.

I was hoping there was some magical way to transform the key/value Map returned from the request object into what I needed by swapping the keys and values with a few keystrokes.

Tom
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tom,

The first option sounds simpler to me, particulary since it sounds like it needs to be only one-time operation (i.e. since you're not maintaining the map and adding more elements to it later).

If they are indeed rankings (1, 2, 3, ...), what about just slotting each user ID into its appropriate position in an array?

-Jeff-
 
Ranch Hand
Posts: 820
IntelliJ IDE Tomcat Server VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This may be what you are looking for:

I was just messing around with the Jakarta Commons Collections class BidiMap (short for bidirectional map) . This is a reversible Map, made to get a key by a value and a value by a key. I think it is exactly the DualMap class that Jeff suggested. It maintains an inverse copy of your map accessible by the method call inverseBidiMap().

Jakarta Commons Collections user guide
 
Jeff Langr
author
Ranch Hand
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good call, I need to check out Jakarta Commons more often.

-Jeff-
 
Tom Blough
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Tim. That sounds like the ticket.

Jeff, only problem with using an array is the web values are strings. So it is possible to use any character for ranking (ie a-zA-Z0-9 and others). In addition, the user could possibly use 1,2,3,5,6 for ranking.

Thanks, All.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!