java 获取 HashMap 中值的大小/长度

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

Get size/length of a value in a HashMap

javasizehashmap

提问by user1621988

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

How do I get the size/length of matching value's at the String?

如何在字符串中获取匹配值的大小/长度?

example:

例子:

1 , Red 2 , Red 3 , Blue 4 , Blue 5 , Red

1、红色 2、红色 3、蓝色 4、蓝色 5、红色

Size of the String of RED = 3

RED 字符串的大小 = 3

回答by kennytm

Use .values()to get a collection containing all the values of the hash map, and then use Collections.frequency()to count the number of objects in the collection.

使用.values()获取包含所有的哈希表的值的集合,然后用Collections.frequency()计算集合中的对象的数量。

return Collections.frequency(map.values(), "red");

回答by RNJ

Or do you mean the count?

还是你的意思是计数?

If so call

如果是这样打电话

map.values()

to get a list of the values.

获取值列表。

Then you can iterate through it and count how many time RED appears

然后你可以遍历它并计算RED出现的次数