2014-02-15 10 views
0

遍歷我想通過在列表 我能拿到鑰匙,其全部values.But如何讓每個每個值通過這樣做來遍歷單鍵值。如何通過對象我有</p> <pre><code>Map<String, List<Attribute>> binList = new HashMap<String, List<Attribute>>(); </code></pre> <p>中的Hashmap

BinList {3=[index=0 {from=1.3,to=2.42}, index=1 {from=2.42,to=3.54}, index=2 {from=3.54,to=4.66}, index=3 {from=4.66,to=5.78}, index=4 {from=5.78,to=6.9}], 2=[index=0 {from=2.3,to=2.76}, index=1 {from=2.76,to=3.2199999999999998}, index=2 {from=3.2199999999999998,to=3.6799999999999997}, index=3 {from=3.6799999999999997,to=4.14}, index=4 {from=4.14,to=4.6}], 1=[index=0 {from=4.3,to=5.02}, index=1 {from=5.02,to=5.739999999999999}, index=2 {from=5.739999999999999,to=6.459999999999999}, index=3 {from=6.459999999999999,to=7.179999999999999}, index=4 {from=7.179999999999999,to=7.899999999999999}], 4=[index=0 {from=0.3,to=0.76}, index=1 {from=0.76,to=1.2200000000000002}, index=2 {from=1.2200000000000002,to=1.6800000000000002}, index=3 {from=1.6800000000000002,to=2.14}, index=4 {from=2.14,to=2.6}]} 



Map<String, List<Attribute>> binList = new HashMap<String, List<Attribute>>(); 
System.out.println("BinList "+binList); 
//Iterating binList 
while (it.hasNext()) { 
    Map.Entry pairs = (Map.Entry)it.next(); 
    System.out.println("->>>>"+pairs.getKey() + " = " + pairs.getValue()); 
    } 

輸出

->>>>3 = [index=0 {from=1.3,to=2.42}, index=1 {from=2.42,to=3.54}, index=2 {from=3.54,to=4.66}, index=3 {from=4.66,to=5.78}, index=4 {from=5.78,to=6.9}] 
->>>>2 = [index=0 {from=2.3,to=2.76}, index=1 {from=2.76,to=3.2199999999999998}, index=2 {from=3.2199999999999998,to=3.6799999999999997}, index=3 {from=3.6799999999999997,to=4.14}, index=4 {from=4.14,to=4.6}] 
->>>>1 = [index=0 {from=4.3,to=5.02}, index=1 {from=5.02,to=5.739999999999999}, index=2 {from=5.739999999999999,to=6.459999999999999}, index=3 {from=6.459999999999999,to=7.179999999999999}, index=4 {from=7.179999999999999,to=7.899999999999999}] 
->>>>4 = [index=0 {from=0.3,to=0.76}, index=1 {from=0.76,to=1.2200000000000002}, index=2 {from=1.2200000000000002,to=1.6800000000000002}, index=3 {from=1.6800000000000002,to=2.14}, index=4 {from=2.14,to=2.6}] 

如何通過價值

id =3 
    index=0 {from=1.3,to=2.42} 
    index=1 {from=2.42,to=3.54} 
. 
. 
+1

downvoters請comment.So我可以提高我的問題 –

+0

您已經越來越爲每單價值你的鑰匙。一個鍵被映射到一個單一的值。每個條目都有一個鍵和一個值。恰巧你的價值觀是集合。 –

回答

6

你有一個迭代Map包含是List<Attribute>實例值。

如果你想遍歷這些列表並顯示它們的內容......你需要遍歷這些列表;

for (Map.Entry<String, List<Attribute>> entry : binList.entrySet()) 
{ 
    System.out.println("Key: " + entry.getKey()); 

    // Each value is a List<Attribute>, so you can iterate though that as well 
    for (Attribute a : entry.getValue()) 
    { 
     // This assumes Attribute.toString() prints something useful 
     System.out.println("Attribute: " + a); 
    } 
} 

編輯補充:您展示在你的榜樣的Iterator,而你使用原始類型,而不是仿製藥。以上消除Iterator,但大概這是您正在嘗試使用Iterator s的正確版本。上面顯示的「的foreach」循環是等價的:

Iterator<Map.Entry<String, List<Attribute>>> it = binList.entrySet().iterator(); 
while (it.hasNext()) 
{ 
    Map.Entry<String, List<Attribute>> entry = it.next(); 
    System.out.println("Key: " + entry.getKey()); 

    // Each value is a List<Attribute>, so you can iterate though that as well 
    Iterator<Attribute> it2 = entry.getValue().iterator(); 

    while (it2.hasNext()) 
    { 
     Attribute a = it2.next(); 
     // This assumes Attribute.toString() prints something useful 
     System.out.println("Attribute: " + a); 
    } 
} 
+0

+1有些時候你只是字面意思。 –

+1

+1,我只是想提出這個:) –

1

嘗試用foreach風格的Java

for (String entryKey:binList.keySet()){ 
    for (List<Attribute> attribute:binList.get(entryKey)){ 
     attribute.from 
     attribute.to 
    } 
} 
相關問題