HashMap: single key, multiple values

I was wondering how I can get the third value for the first key on this card. Is it possible? Sorry if this is repeated, but I did not find anything.

+50
java
Nov 22 '11 at 15:41
source share
10 answers

Libraries exist for this, but the easiest simple way for Java is to create a Map of List as follows:

 Map<Object,ArrayList<Object>> multiMap = new HashMap<Object,ArrayList<Object>>(); 
+61
Nov 22 '11 at 15:44
source share

It looks like you are looking for multimap . Guava has various Multimap implementations, usually created using Multimaps .

I would suggest that using this implementation is likely to be easier than minimizing your own, working out what the API should look like, carefully checking the existing list when adding a value, etc. If your situation has a particular aversion to third party libraries, it might be worth doing it, but otherwise Guava is a fabulous library that will probably help you with other code too :)

+26
Nov 22 '11 at 15:44
source share

For example:

 Map<Object,Pair<Integer,String>> multiMap = new HashMap<Object,Pair<Integer,String>>(); 

where Pair is a parametric class

 public class Pair<A, B> { A first = null; B second = null; Pair(A first, B second) { this.first = first; this.second = second; } public A getFirst() { return first; } public void setFirst(A first) { this.first = first; } public B getSecond() { return second; } public void setSecond(B second) { this.second = second; } } 
+16
Nov 22 '11 at 16:01
source share

Do you have something like this?

 HashMap<String, ArrayList<String>> 

If so, you can iterate through your ArrayList and get the element you like using arrayList.get (i).

+8
Nov 22 '11 at 15:44
source share

Standard Java HashMap cannot store multiple values ​​for each key, any new entry that you add will overwrite the previous one.

+7
Nov 22 '11 at 15:42
source share

This is what I found in a similar question answer

 Map<String, List<String>> hm = new HashMap<String, List<String>>(); List<String> values = new ArrayList<String>(); values.add("Value 1"); values.add("Value 2"); hm.put("Key1", values); // to get the arraylist System.out.println(hm.get("key1")); 

RESULT: [Value 1, Value 2]

+6
Aug 11 '14 at 19:25
source share

Try using collections to store key values:

 Map<Key, Collection<Value>> 

you need to save the list of values ​​yourself

+4
Nov 22 '11 at 15:44
source share

I found the blog in a random search, I think this will help in this: http://tomjefferys.blogspot.com.tr/2011/09/multimaps-google-guava.html

 public class MutliMapTest { public static void main(String... args) { Multimap<String, String> myMultimap = ArrayListMultimap.create(); // Adding some key/value myMultimap.put("Fruits", "Bannana"); myMultimap.put("Fruits", "Apple"); myMultimap.put("Fruits", "Pear"); myMultimap.put("Vegetables", "Carrot"); // Getting the 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] // Iterating over entire Mutlimap for(String value : myMultimap.values()) { System.out.println(value); } // Removing a single value myMultimap.remove("Fruits","Pear"); System.out.println(myMultimap.get("Fruits")); // [Bannana, Pear] // Remove all values for a key myMultimap.removeAll("Fruits"); System.out.println(myMultimap.get("Fruits")); // [] (Empty Collection!) } } 
+3
Feb 20 '14 at 14:21
source share

The thought of a card with two keys immediately made me use a user-defined key, and it would probably be a class. Below is the key:

 public class MapKey { private Object key1; private Object key2; public Object getKey1() { return key1; } public void setKey1(Object key1) { this.key1 = key1; } public Object getKey2() { return key2; } public void setKey2(Object key2) { this.key2 = key2; } } // Create first map entry with key <A,B>. MapKey mapKey1 = new MapKey(); mapKey1.setKey1("A"); mapKey1.setKey2("B"); 
0
Jan 23 '14 at 11:25
source share

HashMap - single key and multiple values ​​using a list

 Map<String, List<String>> map = new HashMap<String, List<String>>(); // create list one and store values List<String> One = new ArrayList<String>(); One.add("Apple"); One.add("Aeroplane"); // create list two and store values List<String> Two = new ArrayList<String>(); Two.add("Bat"); Two.add("Banana"); // put values into map map.put("A", One); map.put("B", Two); map.put("C", Three); 
0
04 Dec '17 at 19:03
source share



All Articles