collections - Multi-valued hashtable in Java -


Is it possible to have multiple values ​​for the same key in a hash table? If not, can you suggest any class or interface that can be used?

No it is the idea of ​​hash tables.

However, you can either use the map & lt; YourKeyObject, list & lt; YourValueObject & gt; & Gt; You can roll your roll with and use some utility methods to make lists if it does not exist, or use something like that.

Example:

  string key = "hello"; Multimap & lt; String, integer & gt; MyMap = HashMultimap.create (); MyMap.put (key, 1); MyMap.put (key, 5000); Println (myMap.get (key)); // print either "[1, 5000]" or "[5000, 1]" myMap = ArrayListMultimap.create (); MyMap.put (key, 1); MyMap.put (key, 5000); Println (myMap.get (key)); // always prints "[1, 5000]"  

Note that multimap is not accurate as a home-baked solution is ; Synchronizes all of its methods, while multimap does not make such a guarantee, this means that you can have problems using Multimap if you want to Using multiple threads If your map is used only on one thread, then it will not make any difference (and you should have used anyway instead of Hashtable .) .


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -