我正在尝试获取具有最大数量的值(而不是最大值)的钥匙。我尝试了几件事,例如计算对每个键进行迭代的值。但是他们归结为从价值中寻找钥匙的问题,当我们拥有相同的价值时,这是有问题的。
// calculate the length
for(String key : map.keySet())
{
len.add(map.get(key).size());
}
// sort the length
Collections.sort(len, Collections.reverseOrder() );
String maxKey = null;
for (String key : map.keySet())
{
if (maxKey == null || map.get(key).size() > map.get(maxKey).size())
{
maxKey = key;
}
}
这将是我解决这个问题的方法。执行后,maxKey
是具有最大价值的关键。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句