java 在 HashMap 中查找值的数量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35993201/
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
Finding number of values in a HashMap?
提问by java123999
What is the best / most efficient way to find the total number of Valuesin a HashMap
.
什么是发现的总数最好/最有效的方式值的HashMap
。
I do not mean the .size()method as it counts the number of keys. I want the total number of valuesin all the keys.
我的意思不是.size()方法,因为它计算键的数量。我想要所有键中值的总数。
I want to do so as my key is a String
, but my value is a List
.
我想这样做,因为我的键是 a String
,但我的值是 a List
。
采纳答案by Suresh Atta
Easiest would be, iterate and add over list sizes.
最简单的方法是迭代并添加列表大小。
int total = 0;
for (List<Foo> l : map.values()) {
total += l.size();
}
// here is the total values size
回答by Clashsoft
In Java 8, you can also utilize the Stream
API:
在 Java 8 中,您还可以使用Stream
API:
int total = map.values()
.stream()
.mapToInt(List::size) // or (l -> l.size())
.sum()
This has the advantage that you don't have to repeat the List<Foo>
type for a for
variable, as in the pre-Java 8 solution:
这样做的好处是您不必像在 Java 8 之前的解决方案中那样重复变量的List<Foo>
类型for
:
int total = 0;
for (List<Foo> list : map.values())
{
total += list.size();
}
System.out.println(total);
In addition to that, although not advised, you could also use that value inline without needing a temp variable:
除此之外,虽然不建议这样做,但您也可以在不需要临时变量的情况下内联使用该值:
System.out.println(map.values().stream().mapToInt(List::size).sum());
回答by Mureinik
If I understand the question correctly, you have a Map<String, List<Something>>
, and you want to count the total number of items in all the List
s in the Map
's values. Java 8 offers a pretty easy way of doing this by streaming the values, mapping them to their size()
and then just summing them:
如果我正确理解了这个问题,那么您有一个Map<String, List<Something>>
,并且您想计算's 值中所有List
s 中的项目总数Map
。Java 8 通过流式传输值,将它们映射到它们size()
,然后将它们相加,提供了一种非常简单的方法来做到这一点:
Map<String, List<Something>> map = ...;
int totalSize = map.values().stream().mapToInt(List::size).sum());
回答by dinomario10
Say you have a map
假设你有一张地图
Map<String, List<Object>> map = new HashMap<>();
You can do this by calling the values()
method and calling size()
method for all the lists:
您可以通过为所有列表调用values()
方法和调用size()
方法来执行此操作:
int total = 0;
Collection<List<Object>> listOfValues = map.values();
for (List<Object> oneList : listOfValues) {
total += oneList.size();
}
回答by Tunaki
Starting with Java 8, given a Map<K, List<V>> map
you could use the Stream API and have:
从 Java 8 开始,假设Map<K, List<V>> map
您可以使用 Stream API 并具有:
int size = map.values().stream().mapToInt(List::size).sum();
This creates a Stream
of the values with stream()
, maps each of them to their size with mapToInt
, where the mapper is the method reference List::size
refering to List#size()
, and sum the results with sum()
.
这就产生了一个Stream
与所述值stream()
,其中每一个分别映射到它们的大小与mapToInt
,其中所述映射器是该方法的参考List::size
指的List#size()
,总结的结果sum()
。
回答by itohro
With Eclipse Collections, the following will work using MutableMap.
使用Eclipse 集合,以下内容将使用MutableMap工作。
MutableMap<String, List<String>> map =
Maps.mutable.of("key1", Lists.mutable.of("a", "b", "c"),
"key2", Lists.mutable.of("d", "e", "f", "g"));
long total = map.sumOfInt(List::size);
Note: I am a committer for Eclipse Collections.
注意:我是 Eclipse Collections 的提交者。
回答by Mostafa Hussien
import java.util.HashMap;
public class Solution {
public static void main(String args[]) {
int total = 0;
HashMap<String,String> a = new HashMap<String,String>();
a.put("1.","1");
a.put("2.","11");
a.put("3.","21");
a.put("4.","1211");
a.put("5.","111221");
for (String l : a.values()) {
total ++;
}
System.out.println(total);
}
}