Java 具有重复值的映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21620155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 09:41:41  来源:igfitidea点击:

Map with duplicate values

javacollectionshashmap

提问by Prasad

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");

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?

众所周知,我们不能在HashMap. 那么我可以实现这个并根据键检索值吗?或者其他更好的解决方案?

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

如何在HashMap中包含重复的键?| 我如何在 Java 中使用具有重复键的 HashMap

采纳答案by Karthik Surianarayanan

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

Map 不支持重复键。您可以使用集合作为针对同一键的值。

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.

你可以使用这样的东西。

回答by Jadiel de Armas

Type multimapsin 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.

但是找到 multimaps 实现将为您省去所有麻烦。

回答by Bhaskar

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.

它如何将您的问题带入图片,是通过拥有一个可以包含重复项的多图。最后,将 multimap 放入地图中,您会发现在新地图中也添加了重复项的 MultiMap。另外,在 main 方法中找到注释代码,这表明 Map 不能容纳重复项。

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

但是,我发现 Armas 提供的解决方案也是一个非常合适的解决方案。希望能帮助到你 :)