【发布时间】:2016-07-21 17:39:26
【问题描述】:
我一直在尝试修改此处类似问题的最佳答案中的代码,但我无法让它适用于数组列表长度
Get the keys with the biggest values from a hashmap?
假设我有
HashMap<Customer,ArrayList<Call>> outgoingCalls = new HashMap<Customer,ArrayList<Call>>();
当程序运行时,它会将每次调用都存储在 hashmap 中。我想遍历这个哈希图并返回拨打最多电话的客户。我一直在尝试从上面的链接修改此代码,但我完全迷路了
Entry<Customer,ArrayList<Call> mostCalls = null;
for(Entry<String,ArrayList<Call> e : outgoingCalls.entrySet()) {
if (mostCalls == null || e.getValue() > mostCalls.getValue()) {
mostCalls = e;
【问题讨论】:
-
e.getValue()返回一个ArrayList<Call>。mostCalls.getValue()也是如此。 >(大于)适用于数字,而不是像ArrayList这样的集合。