Any data structure in java that supports multiple keys - not map exactly -


i came cross situation need like:

197 => 6   (1 year) 197 => 12  (2 years) 

want add multiple products , price, based on years of subscription, value varies.

i wonder if have data structure in java supports duplicates keys (should behave map must support duplicate keys).

i can creating class wanted know if there supports thing..it more map no map since needs support multiple keys.

yes can using existing libraries guava. here link interface multimap of guava library link. here example on how use (taken here):

  multimap<string, string> mymultimap = arraylistmultimap.create();    // adding key/value   mymultimap.put("fruits", "banana");   mymultimap.put("fruits", "apple");   mymultimap.put("fruits", "pear");   mymultimap.put("vegetables", "carrot");    // getting size   int size = mymultimap.size();   system.out.println(size);  // 4    // getting values   collection<string> fruits = mymultimap.get("fruits");   system.out.println(fruits); // [bannana, apple, pear]    collection<string> vegetables = mymultimap.get("vegetables");   system.out.println(vegetables); // [carrot] 

the second possibility create map<string, list<object>> , manually handling reference list of objects need associate single key.


Comments

Popular posts from this blog

c# - Binding a comma separated list to a List<int> in asp.net web api -

Delphi 7 and decode UTF-8 base64 -

html - Is there any way to exclude a single element from the style? (Bootstrap) -