1

Is there any way to use map or any other collection which allow us to store duplicate keys
with different values...
rather then using a List to store multiple values for same key?

7
  • 3
    Map<Key, List> is definitely the right way to go. Commented Oct 22, 2013 at 13:31
  • 1
    Not that I'm aware of. How would you look up the value of that key if the key pointed to multiple values? Commented Oct 22, 2013 at 13:32
  • I think the best question to ask here is. When you call map.get(key), and the map contains multiple of key. What do you expect to be returned? Commented Oct 22, 2013 at 13:33
  • Implement your own Map, but I don't see meaning in that. What's the logic behind what you seek, that Map with key and list cannot provide? Commented Oct 22, 2013 at 13:35
  • can you give a little view to the scenario you are facing , that brings you to have this requirement Commented Oct 22, 2013 at 13:37

5 Answers 5

8

Use Google Guava's MultiMap. This allows multiple values with single key

http://guava-libraries.googlecode.com/svn/tags/release03/javadoc/com/google/common/collect/Multimap.html

Sign up to request clarification or add additional context in comments.

1 Comment

+1, but OP should realise that this is effectively rather then using a List to store multiple values for same key?
2

Map doesn't allow you to have duplicated keys. That even doesn't make sense.

Possible solution is having list(Collection) of values. Just go for it.If anything stopping you, let us know.

3 Comments

Ok,,Got it going with the approach having list of values with same key Just wanted to know wheather there is any approach better then this..
Not sure about 'any approach better'; this actually sounds like the right approach. Depending on context though, a Map<K,Set<V>> might be better sometimes. Does the order of the values matter? Are there duplicates (for any given key)? Using TreeSet (naturally ordered) or LinkedHashSet (insertion order) might be useful, sometimes, too.
@Paul Understood your intention :) list of values in the sense, Having a collection of values for single key. That selecting Collection is next step. +1. Nice point thought. The first Doubt of OP Pankaj is to go for a Collection or not.
2

There is a Multimap concept. For example in guava. Multimap in guava

But it's not a part of Collection framework.

If you would not like signature like this Map<String, List<Item>>, you could easily wrap it with object. E.g.

class Items {
   private List<Item> items;
   public void add(Item i) {}
}

Of course it would not be possible to add items through map instance as map.add("key", item)

Comments

0

What about the next?:

Map<String, List<String>> map = new HashMap<>();

Add values

// add "key1" and "value1"
if (!map.containsKey("key1")) {
    map.put("key1", new ArrayList<String>());
}
map.get("key1").add("value1");

// add "key1" and "value2"
if (!map.containsKey("key1")) {
    map.put("key1", new ArrayList<String>());
}
map.get("key1").add("value2");

Get values

List<String> values = map.get("key1");

This with string, but can be for any type. And you don't need additional libraries.

Comments

0

you can use MultiMap from apache commons collections

http://commons.apache.org/proper/commons-collections/javadocs/api-3.2.1/org/apache/commons/collections/MultiMap.html

Comments

Your Answer

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.