• Post Reply Bookmark Topic Watch Topic
  • New Topic

Is there a Properties-like class that allows multiple values per key?  RSS feed

 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello fello Java folk,

I know this would be really simple to implement, but I'm wondering if a standard implementation already exists. I'm looking for a class like Properties, but which maps a list of string values to a string key. Sort of like the way ServletRequest handles parameters, but with ability to add and remove new ones. I need the methods getPropertyValues(key), addPropertyValue(key, value) and removePropertyValue(key, value). When the last value of a key is removed, the key itself is removed from the collection.

I'm only going to be using this with Strings, but if an implementation exists only for Objects, casting will not present a problem.

Thank you in advance,
Yuriy
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could write one with a Map of Sets.

Does that make enough sense to figure out get and remove? Then you could make a class that loads and saves as text or XML. Could be kinda fun
 
Yuriy Zilbergleyt
Ranch Hand
Posts: 429
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think I'll try this. Thanks!

-Yuriy
 
James Carman
Ranch Hand
Posts: 580
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or, if you can use Jakarta Commons Collections, there's a class called MultiHashMap in there that does exactly what you're asking for.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!