【问题标题】:How to iterate through objects in Hashmap如何遍历Hashmap中的对象
【发布时间】:2014-02-15 04:10:31
【问题描述】:

我有

Map<String, List<Attribute>> binList = new HashMap<String, List<Attribute>>();

我想遍历列表中的每个值 通过这样做,我能够获取键及其整个值。但是如何获取键的每个单个值。

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}
.
.

【问题讨论】:

  • downvoters 请评论。所以我可以改进我的问题
  • 您已经获得了密钥的每个值。键映射到单个值。每个条目都有一个键和一个值。碰巧你的价值观是集合。

标签: java list hashmap


【解决方案1】:

您有一个 Map,其中包含作为 List&lt;Attribute&gt; 实例的值。

如果您想遍历这些列表并显示它们的内容...您需要遍历这些列表;

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,但大概这是您尝试使用Iterators 所做的正确版本。上面显示的“for-each”循环是等价的:

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);
    }
}

【讨论】:

    【解决方案2】:

    在 java 中尝试 foreach 样式

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

    【讨论】:

      猜你喜欢
      • 2022-01-16
      • 2011-10-23
      • 2012-10-16
      • 2022-01-24
      • 2012-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多