如何遍历Hashmap中的对象 [英] How to iterate through objects in Hashmap

查看:189
本文介绍了如何遍历Hashmap中的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有

I am having

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

我想遍历列表
中的每个值通过这样做,我可以获得键和它的整个值。但是如何获得每个键的单个值。

I want to iterate through each values in list By doing this I am able to get the key and its entire values.But how to get each single value for a key.

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

我如何terate through values

How to iterate through values

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


推荐答案

/ code>,其中包含 List< Attribute> 的实例值。

You have a Map that contains values that are instances of List<Attribute>.

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

If you want to iterate though those lists and display their contents... you'd need to iterate through those lists;

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的正确版本。上面显示的for-each循环是等价的:

Edit to add: You show an Iterator in your example, and you're using raw types instead of generics. The above does away with the Iterator but presumably this is the correct version of what you were trying to do using Iterators. The "for-each" loops shown above are equivalent:

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

这篇关于如何遍历Hashmap中的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆