[英]how to get the size of array inside a hashmap in java
I have a new ArrayList
inside a HashMap
, but i don't know how to calculate the size of ArrayList
inside the HashMap
我在
HashMap
有一个新的ArrayList
,但我不知道如何计算HashMap
的ArrayList
的大小
Map<String, List<fruit>> groupbyregion = new HashMap<>();
for (fruit s : ll) {
if (!groupbyregion.containsKey(s.getRegionName())) {
groupbyregion.put(s.getRegionName(), new ArrayList<>());
}
groupbyregion.get(s.getRegionName()).add(s);
}
System.out.println("Group by region: " + groupbyregion);
You are already using: 您已经在使用:
groupbyregion.get(s.getRegionName()).add(s);
This calls the add() method of the ArrayList in that map slot. 这将在该映射插槽中调用ArrayList的add()方法。 Thus:
从而:
groupbyregion.get(s.getRegionName()).size();
gives you the size. 给你的大小。
Beyond that: you want to look here for how to create lists within maps in more elegant (java8) ways. 除此之外:您想在这里看看如何以更优雅的(java8)方式在地图内创建列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.