2

Is it possible to keep multiple values corresponding to a key in a HashMap? If yes, how?

DNA
42.7k12 gold badges114 silver badges153 bronze badges
asked Sep 21, 2012 at 19:04
2
  • 1
    for example, by storing container type, such as List, as value. Commented Sep 21, 2012 at 19:07
  • possible duplicate of HashMap: One Key, multiple Values Commented Sep 21, 2012 at 19:09

6 Answers 6

11

Yes, this is called chaining. You will want to avoid chaining as much as possible, especially if the size of the chain starts increasing. Longer chain size will counter the whole purpose of using a hash structure because the goal is to come as close to O(1) as possible.

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);
Steve Kuo
63.2k75 gold badges199 silver badges260 bronze badges
answered Sep 21, 2012 at 19:13

1 Comment

how can i get the values of "List" for key "Key1"
9

You could give a shot at Guava library (former Google collections). It has implementations of Multimaps which can store multiple values for a single key.

For example ListMultimap implementations allow duplicate key/value pairs which are kept in insertion order.

Here's how you'd use it:

ListMultimap<String, Integer> numberClasses = ArrayListMultimap.create();
numberClasses.put("odd", 1);
numberClasses.put("odd", 3);
numberClasses.put("odd", 5);
numberClasses.put("even", 2);
numberClasses.put("even", 4);
numberClasses.put("even", 6);
assertEquals(Arrays.asList(1,3,5), numberClasses.get("odd"));
assertEquals(Arrays.asList(2,4,6), numberClasses.get("even"));

Another cool example would be SetMultimap, which is very similar to ListMultimap except that values for a key are kept in a set. (From user perspective, I don't know how exactly it is implemented.)

SetMultimap<String, Integer> setMultimap= HashMultimap.create();
setMultimap.put("key1", 1);
setMultimap.put("key1", 1);
setMultimap.put("key1", 1);
setMultimap.put("key1", 2);
setMultimap.put("key2", 1);
setMultimap.put("key2", 3);
assertEquals(ImmutableSet.of(1,2), setMultimap.get("key1"));
assertEquals(ImmutableSet.of(1,3), setMultimap.get("key2"));
Iulian Popescu
2,6434 gold badges25 silver badges31 bronze badges
answered Sep 21, 2012 at 19:14

Comments

1

Use Map<String, List<String>>.

answered Sep 21, 2012 at 19:06

1 Comment

or any other container, better suited for your task. For example, Set. Or just plain old array.
0

Strictly speaking, no.

But! You can have as your value, some kind of Collection and use that to store as many values as you wish.

answered Sep 21, 2012 at 19:07

Comments

0

Yes, but only if the value type being stored in your Map is an array or List:

Map<String, List<String>> myMap

or

Map<String, String[]> myMap

But it's generally bad practice to build up generic data structures inside generic data structures.

Why not write a domain-specific class that wraps the HashMap, and makes it easier for you to check for existence of a value, number of items per key, etc?

answered Sep 21, 2012 at 19:08

Comments

0

Without using any other Libary Create a map with Key as String and value as HashSet value will be not repeated string.


multivaleMap.computeIfAbsent("key", k -> new HashSet<String>()).add("value1");
multivaleMap.computeIfAbsent("key", k -> new HashSet<String>()).add("value2");
System.out.println(multivaleMap);``` will print {key=[value2, value1]}
answered Jul 20, 2020 at 20:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.