This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes need help with Collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "need help with Collections" Watch "need help with Collections" New topic
Author

need help with Collections

Nick Kocher
Greenhorn

Joined: Nov 24, 2004
Posts: 14
Hi,
I need a method that would change the keys and values of the Properties instance so that keys become values and vice versa.

I wrote my own version, but I think there must be already such a method somethere in Collections and I want to use it.
Stefan Krompass
Ranch Hand

Joined: Apr 29, 2004
Posts: 75
Hi,

there is no such method in the API since it would only work for a subset of all properties. Imagine the following case:

When your values become keys, they are no longer unique.

Stefan
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by Nick Kocher:
I wrote my own version, but I think there must be already such a method somethere in Collections and I want to use it.
There is no such API method that I am aware of, for it's certainly an odd thing to do to a Map/Properties (not that it couldn't be useful). Also, it has the problem that Stefan mentioned.

However, you can at least improve your method to iterate over the paired Map.Entry objects. I haven't looked at the Properties source, so I'm assuming it implements the Map interface. Finally, I recommend building a new Collection rather than modifying the one in-place.
 
Consider Paul's rocket mass heater.
 
subject: need help with Collections
 
Similar Threads
elegant way to reverse key/value in Map
Map.Entry Details help
New mock tests!
what collection class to use??
Iterator in HashMap