• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need help with Collections

 
Nick Kocher
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic