2016-04-01 8 views
0

私はこれと同様の質問から上の回答からコードを修正しようとしていますが、私はそれをarraylistの長さJava - ハッシュマップで最も長いArrayListを持つキーを返す<Class、ArrayList <Class>>

Get the keys with the biggest values from a hashmap?

私はプログラムが実行されると、それはハッシュマップで行われたすべての呼び出しを格納

HashMap<Customer,ArrayList<Call>> outgoingCalls = new HashMap<Customer,ArrayList<Call>>();

を考えてみましょう。このハッシュマップを実行して、最も多くの呼び出しを行った顧客を返したいと思います。しかしかなり、私は上記のリンクから、このコードを変更しようとしてきたが、私は完全に

Entry<Customer,ArrayList<Call> mostCalls = null; 

    for(Entry<String,ArrayList<Call> e : outgoingCalls.entrySet()) { 
    if (mostCalls == null || e.getValue() > mostCalls.getValue()) { 
     mostCalls = e; 
+0

'e.getValue()'は 'ArrayList 'を返します。 'mostCalls.getValue()'も同様です。 >(より大きい)は、 'ArrayList'のようなコレクションではなく、数字に対して機能します。 –

答えて

5

閉じるを失っています。

Entry<Customer,ArrayList<Call>> mostCalls = null; 

for(Entry<String,ArrayList<Call>> e : outgoingCalls.entrySet()) { 
    if (mostCalls == null || e.getValue().size() > mostCalls.getValue().size()) { 
     mostCalls = e; 
    } 
} 
+0

ありがとう!私が言及することを忘れたことの一つは、「シンボルを見つけることができません:エントリー」エラーです。私は 'java.util。*'はそれをインポートするのに十分なはずだと思ったが、何か他に何かする必要があるのだろうか? –

+0

'java.util.Map.Entry'をインポートするか、' Entry'の代わりに 'Map.Entry'を使う必要があります。 –

+0

パーフェクト、ありがとう。私はまだこれを実際に表示するためにコードに取り組まなければならないが、私は良い兆候として取るだろうコンパイラのエラーを取得していない。 –

0

これを試すことができます。余分な輸入は必要ありません。

int maxSize = Integer.MIN_VALUE; 
for(Customer e: outgoingCalls.keySet()) { 
    if (maxSize < outgoingCalls.get(e).size()) { 
     maxSize = outgoingCalls.get(e).size(); 
     mostCalls = e; 
    } 
} 
0
public class T { 
    public static void main(String[] args) { 
    List<Customer> customerList = new ArrayList<Customer>(); 
    customerList.add(new Customer()); 
    Collections.sort(customerList, new Comparator<Customer>() { 
     @Override 
     public int compare(Customer c1, Customer c2) { 
     return c1.callsMadeByCustomer.size() - c2.callsMadeByCustomer.size(); 
     } 
    }); 
    System.out.println("Most Calls: " + customerList.get(customerList.size() - 1)); 
    } 
} 

class Customer { 
    ArrayList<Call> callsMadeByCustomer; 

    public Customer() { 
     callsMadeByCustomer = new ArrayList<Call>(); 
    } 
} 

あなたも、このようにそれを整理することができます。したがって、callsMadeByCustomerは顧客クラスの内部にあります。

関連する問題