HashMap with multiple values under the same key
You could:
- Use a map that has a list as the value.
Map<KeyType, List<ValueType>>
. - Create a new wrapper class and place instances of this wrapper in the map.
Map<KeyType, WrapperType>
. - Use a tuple like class (saves creating lots of wrappers).
Map<KeyType, Tuple<Value1Type, Value2Type>>
. - Use mulitple maps side-by-side.
Examples
1. Map with list as the value
// create our map
Map<String, List<Person>> peopleByForename = new HashMap<>();
// populate it
List<Person> people = new ArrayList<>();
people.add(new Person("Bob Smith"));
people.add(new Person("Bob Jones"));
peopleByForename.put("Bob", people);
// read from it
List<Person> bobs = peopleByForename["Bob"];
Person bob1 = bobs[0];
Person bob2 = bobs[1];
The disadvantage with this approach is that the list is not bound to exactly two values.
2. Using wrapper class
// define our wrapper
class Wrapper {
public Wrapper(Person person1, Person person2) {
this.person1 = person1;
this.person2 = person2;
}
public Person getPerson1() { return this.person1; }
public Person getPerson2() { return this.person2; }
private Person person1;
private Person person2;
}
// create our map
Map<String, Wrapper> peopleByForename = new HashMap<>();
// populate it
peopleByForename.put("Bob", new Wrapper(new Person("Bob Smith"),
new Person("Bob Jones"));
// read from it
Wrapper bobs = peopleByForename.get("Bob");
Person bob1 = bobs.getPerson1();
Person bob2 = bobs.getPerson2();
The disadvantage to this approach is that you have to write a lot of boiler-plate code for all of these very simple container classes.
3. Using a tuple
// you'll have to write or download a Tuple class in Java, (.NET ships with one)
// create our map
Map<String, Tuple2<Person, Person> peopleByForename = new HashMap<>();
// populate it
peopleByForename.put("Bob", new Tuple2(new Person("Bob Smith",
new Person("Bob Jones"));
// read from it
Tuple<Person, Person> bobs = peopleByForename["Bob"];
Person bob1 = bobs.Item1;
Person bob2 = bobs.Item2;
This is the best solution in my opinion.
4. Multiple maps
// create our maps
Map<String, Person> firstPersonByForename = new HashMap<>();
Map<String, Person> secondPersonByForename = new HashMap<>();
// populate them
firstPersonByForename.put("Bob", new Person("Bob Smith"));
secondPersonByForename.put("Bob", new Person("Bob Jones"));
// read from them
Person bob1 = firstPersonByForename["Bob"];
Person bob2 = secondPersonByForename["Bob"];
The disadvantage of this solution is that it's not obvious that the two maps are related, a programmatic error could see the two maps get out of sync.
HashMap: One Key, multiple Values
Libraries exist to do this, but the simplest plain Java way is to create a Map
of List
like this:
Map<Object,ArrayList<Object>> multiMap = new HashMap<>();
HashMap with multiple Value
You can use a Multimap instead. It keeps multiple values for a key in a list. There are implementations in commons-collections and in Guava.
Multimap<String, String> multimap = ArrayListMultimap.create();
multimap.put("ducks", "Huey");
multimap.put("ducks", "Dewey");
multimap.put("ducks", "Louie");
Collection<String> ducks = multimap.get("ducks");
System.out.println(ducks); // [Huey, Dewey, Louie]
It is similar to using a Hashmap where the values are lists, but you don't have to explicitly create the lists.
The same example done the do-it-yourself way looks like:
Map<String, List<String>> map = new HashMap<>();
map.put("ducks", new ArrayList<String>());
map.get("ducks").add("Huey");
map.get("ducks").add("Dewey");
map.get("ducks").add("Louie");
// or as an alternative to the prev 4 lines:
// map.put("ducks", new ArrayList<String>(
// new String[] {"Huey", "Dewey", "Louie"}));
Collection<String> ducks = map.get("ducks");
System.out.println(ducks); // [Huey, Dewey, Louie]
Note that you can use the Multimap as a builder and call asMap on it to return a map.
Related Topics
Causes of Getting a Java.Lang.Verifyerror
What Causes Javac to Issue the "Uses Unchecked or Unsafe Operations" Warning
How to Compare Objects by Multiple Fields
Getting a File'S Md5 Checksum in Java
Equals VS Arrays.Equals in Java
Why Is the Java Date API (Java.Util.Date, .Calendar) Such a Mess
Syntax For Creating a Two-Dimensional Array in Java
How to Access Java-Classes in the Default-Package
Why Are Arrays Covariant But Generics Are Invariant
Making a Robust, Resizable Swing Chess Gui
Including Dependencies in a Jar With Maven
How to Convert Number to Words in Java
How to Make an Executable Jar File
Jcomponents Not Showing Up With Picture Background
How to Secure an API Rest For Mobile App (If Sniffing Requests Gives You the "Key")