如何获取地图中值的大小?
How to get the size of the values in a Map?
我有一个集合,我在程序开始时加载它,我想当然地认为我用作值的字符串数组对于所有条目将始终具有相同的长度,而且它至少包含 1 个值。
从这些条目中,我需要获取我使用的数组的大小。到目前为止,我是这样做的:
Map<String, String[]> people = new HashTable<>();
people.put("miao", new String[]{"bau","cip","muu"});
people.values().iterator().next().length; //returns 3
有更好的方法吗?
利用 Guava 的 ListMultimap:
ListMultimap<String, String> people = ArrayListMultimap.create();
people.putAll("miao", new ArrayList("bau","cip","muu");
people.get("miao").size();//returns 3
根据@Eran(在评论中)我已经在问题中回答了我自己的问题,因此,要获取值的大小(如果它是一个数组) 做:
Map<String, String[]> people = new HashTable<>();
people.put("miao", new String[]{"bau","cip","muu"});
people.values().iterator().next().length; //returns 3
我有一个集合,我在程序开始时加载它,我想当然地认为我用作值的字符串数组对于所有条目将始终具有相同的长度,而且它至少包含 1 个值。
从这些条目中,我需要获取我使用的数组的大小。到目前为止,我是这样做的:
Map<String, String[]> people = new HashTable<>();
people.put("miao", new String[]{"bau","cip","muu"});
people.values().iterator().next().length; //returns 3
有更好的方法吗?
利用 Guava 的 ListMultimap:
ListMultimap<String, String> people = ArrayListMultimap.create();
people.putAll("miao", new ArrayList("bau","cip","muu");
people.get("miao").size();//returns 3
根据@Eran(在评论中)我已经在问题中回答了我自己的问题,因此,要获取值的大小(如果它是一个数组) 做:
Map<String, String[]> people = new HashTable<>();
people.put("miao", new String[]{"bau","cip","muu"});
people.values().iterator().next().length; //returns 3