1

I have gone through questions relate to the same questions, but I am not getting a clear idea of how to implement the map with duplicate entries. I was asked this question in my interview. Please give me some guidelines.

For instance, a company has two employees of same name and same area of development viz. map.put("Tom Hank","Java");

map.put("Tom Hank","Java");

As we all know, we cannot store duplicate values in HashMap. So can I implement this and retrieve the values according to key? Or any other better solution?

These are the questions that I have gone through but not able to understand how to do this.

How to include duplicate keys in HashMap? | How can i have a HashMap in Java with duplicate keys

Community
  • 1
  • 1
Prasad
  • 1,188
  • 3
  • 11
  • 29
  • What didn't you understand from those questions? The answers there even included working example. – Rohit Jain Feb 07 '14 at 05:41
  • The answers were related to Google's libraries, but my interviewer might be interested in the logic. – Prasad Feb 07 '14 at 05:43
  • 1
    Why not use a Map>? – Jeremy Feb 07 '14 at 05:44
  • 1
    Did your interviewer really told you to use a map? I mean, I' would definitely create an `Employee` class, have a `Set` as an attribute in it, storing area of development, and then maintain a `Set`. – Rohit Jain Feb 07 '14 at 05:45
  • "I am not getting a clear idea of how to implement the map with duplicate entries" - `Map`s don't have duplicate keys, end of story. Either your interviewer was playing a joke on you, or you haven't given us the whole question. – Chris Martin Feb 07 '14 at 05:48
  • You _can_ have the same _values_ just not the same _keys_ – Paul Samsotha Feb 07 '14 at 05:48
  • @RohitJain Thank you mate, but can you elaborate it more, so that I can answer the same question (if asked) next time. – Prasad Feb 07 '14 at 06:52
  • @peeskillet He was saying that I need to put two same keys in a map. – Prasad Feb 07 '14 at 06:54
  • Great idea. But How do you tell which Tom Hank is the right one? – Witold Kaczurba Nov 03 '16 at 07:51
  • @Vito Adding some identification to it like `unique ID` or some `EmployeeId` – Prasad Nov 10 '16 at 12:40
  • @Prasad That's the trick. In my view using non-unique IDs in similar scenarios can lead to problems. Databases/SQLs operate on unique keys so things are well defined and without ambiguity. I think this is probably worth mention answering questions. – Witold Kaczurba Nov 11 '16 at 09:18

3 Answers3

4

Map does not supports duplicate keys. you can use collection as value against same key.

Because if the map previously contained a mapping for the key, the old value is replaced by the specified value.

Map<Integer, List<String>>

you can use something like this.

4

I hope this will help you:

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections.MultiHashMap;
import org.apache.commons.collections.MultiMap;
import org.apache.commons.collections.map.HashedMap;

public class TryingMultiMap {
    public Map<String, String> testMap(MultiMap m) {
        Map<String, String> m_copy = new HashMap();
        m_copy.putAll(m);
        System.out.println(m_copy);
        return m_copy;
    }

    public static void main(String[] args) {
        System.out.println("test");
        MultiMap m = new MultiHashMap();
        m.put("Bhaskar","Java");
        m.put("Bhaskar","Java");
        m.put("Ravi",".Net");
        m.put("Ravi", ".Net");
        m.put("Suyash","C++");
        System.out.println(m);

        /*Map exhibitMap= new HashMap();
        h.put("Bhaskar", "Java");
        h.put("Bhaskar", "Java");
        System.out.println("exhibitMap"+ExhibitMap)*/;

        TryingMultiMap obj = new TryingMultiMap();
        obj.testMap(m);
    }

}

How it brings in your problem into picture, is by having a multimap which can contain duplicates. Finally, multimap is put in the map and you find the MultiMap with duplicates added in the new Map too. Also, find the commented code in the main method, which exhibits the fact that Map cannot hold duplicates.

However, I find the solution provided by Armas is also a very appropriate one. Hope it helps :)

Bhaskar
  • 337
  • 6
  • 21
2

Type multimaps in Google, and you will find what you are looking for.

Another way of doing it would be doing a simulated multimap the following way:

Map<String, List<String>> multimap=new Map<String, List<String>>();
if (multimap.contains("Tom Hanks"){
   multimap.get("Tom Hanks").add("value2");
}
else {
   List<String> values=new ArrayList<String>();
   values.add("value1");
   multimap.put("Tom Hanks", values);
}

But finding a multimaps implementation will save you all the trouble.

Steve Kuo
  • 61,876
  • 75
  • 195
  • 257
Jadiel de Armas
  • 8,405
  • 7
  • 46
  • 62