java 计算Hashmap中的重复值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27254302/
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-11-02 11:30:13  来源:igfitidea点击:

Counting duplicate values in Hashmap

javaarraylisthashmap

提问by user2379090

I have a hash map that looks like this:

我有一个看起来像这样的哈希图:

HashMap<String, ArrayList<String>> varX = new HashMap<String, ArrayList<String>>();

And I can't for the life of me work out how to count the number of duplicate values. For example, If put("001", "DM");into the hash map and put("010", "DM");as well, how can count if there are two values int the ArrayList section of the Hashmap.

而且我一生都无法弄清楚如何计算重复值的数量。例如,如果put("001", "DM");进入哈希映射,put("010", "DM");那么如何计算哈希映射的 ArrayList 部分是否有两个值。

For example, the output would look something like this:

例如,输出将如下所示:

DM:2as I 'put' two DM values into the Hashmap.

DM:2当我将两个 DM 值“放入”Hashmap 时。

回答by nem035

You have a HashMap that maps Stringto ArrayList<String>.

你有一个 HashMap 映射StringArrayList<String>.

Doing put("001", "DM")on this map will not work as was pointed out to you in the comments by @Sotirios Delimanolis.

put("001", "DM")这个地图将无法正常工作的指出你在所作的评论@Sotirios Delimanolis

You would get an error that looks like:

你会得到一个看起来像这样的错误:

The method put(String, ArrayList<String>) in the type HashMap<String,ArrayList<String>> is not applicable for the arguments (String, String)

Based on your example behavior, you want a HashMapthat maps Stringto String(i.e. put("001", "DM");

根据您的示例行为,您想要一个HashMap映射StringString(即put("001", "DM");

Now, assuming you have that:

现在,假设你有:

HashMap<String, String> varX = new HashMap<String, String>();

And you want to count how many keys map to the same value, here's how you can do that:

并且您想计算有多少键映射到相同的 value,您可以这样做:

varX.put("001", "DM");
varX.put("010", "DM");

// ...

int counter = 0;
String countingFor = "DM";
for(String key : varX.keySet()) {            // iterate through all the keys in this HashMap
    if(varX.get(key).equals(countingFor)) {  // if a key maps to the string you need, increment the counter
        counter++;
    }
}
System.out.println(countingFor + ":" + counter);  // would print out "DM:2"

回答by Saravanan R

HashMap hm =new HashMap();
hm.put("01","one");
hm.put("02","two");
hm.put("03","one");
hm.put("04","one");
hm.put("05","two");
HashMap newHm = new  HashMap();
         Iterator it = hm.entrySet().iterator();
            while (it.hasNext()) {
                Map.Entry pair = (Map.Entry)it.next();
                System.out.println(pair.getKey() + " = " + pair.getValue());
                if(newHm.containsKey(pair.getValue())){
                    newHm.put(pair.getValue(), Integer.parseInt(newHm.get(pair.getValue()).toString())+1 );
                }else{
                    newHm.put(pair.getValue(),1 );
                }
                it.remove(); // avoids a ConcurrentModificationException
            }

回答by Imdad Soomro

Collections.frequency(map, "value");is used to count the passed object in collection. Following is the declaration of that method:

Collections.frequency(map, "value");用于统计集合中传递的对象。以下是该方法的声明:

public static int frequency(Collection<?> c, Object o)

回答by MartaGom

As Sotirios says, you can only put an ArrayList.

正如 Sotirios 所说,您只能放置一个 ArrayList。

ArrayList<String> names= new ArrayList<String>();
names.add("Josh");
put("001", names);

If you want to insert Strings into the HashMap, define it as follow:

如果要将字符串插入到 HashMap 中,请定义如下:

Map<String,String> example = new HashMap<String,String>();
example.put( "001", new String( "Rooney" ));

Regards,

问候,