2014-02-15 49 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 
    } 
} 
相关问题