在TreeMap中获取HashMap的值

时间:2015-01-20 02:57:25

标签: java

我有一个存储HashMap的TreeMap。我觉得我应该能够找到这个,但我似乎无法在Google上找到它。

我有一个存储在其中的HashMap的TreeMap,我会这样迭代:

                    while (i.hasNext()) {
                        Map.Entry me = (Map.Entry) i.next();
                        System.out.print(me.getKey() + ": ");
                        System.out.println(me.getValue());
                    }

这将打印输出(示例行):

  

I / System.out:32: {walking = 32,pic = http://****/images/walkers/chase.png,name = Chase,dist = 6096.8589024135445}

我想知道如何从这个HashMap中抓取pic,name,dist。

编辑:我不明白人们错过了哪一点。我把一个HashMap放到了TreeMap中。 TreeMap内部是一个HashMap。我想我可以告诉你什么是HashMap,但你们已经知道了!

TreeMap dist_mp=new TreeMap();
Map<String, String> mp1 = new HashMap<String,String>();
mp1.put("dist", distanceInMiles + "");
mp1.put("name", obj.getString("first_name"));
mp1.put("pic", obj.getString("pic"));
mp1.put("walks", obj.getString("walks"));
dist_mp.put(distanceInMiles, mp1);

5 个答案:

答案 0 :(得分:2)

您需要的只是将TreeMap值转换为Map

while (i.hasNext()) {
    Map.Entry me = (Map.Entry) i.next();
    System.out.print(me.getKey() + ": ");

    // Cast to a Map again
    Map<String, String> mp = (Map<String, String>) me.getValue();

    // get() works now
    System.out.print("name = " + mp.get("name"));
    System.out.print("pic = " + mp.get("pic"));
    System.out.println("dist = " + mp.get("dist"));
}

答案 1 :(得分:1)

需要迭代两次,一次针对TreeMap,然后针对HashMap

 public static void main(String[] args) {
        TreeMap<String, Map<String, String>> dist_mp = new TreeMap<String, Map<String, String>>();
        Map<String, String> mp1 = new HashMap<String, String>();
        mp1.put("dist", "6096.8589024135445");
        mp1.put("name", "Chase");
        mp1.put("pic", "http://****/images/walkers/chase.png");
        mp1.put("walks", "32");
        dist_mp.put("32", mp1);

        for (Map.Entry<String, Map<String, String>> entry : dist_mp.entrySet()) {
            String key = entry.getKey();
            System.out.println(key);
            Map<String, String> myMap = entry.getValue();

            for (Map.Entry<String, String> entry1 : myMap.entrySet()) {
                System.out.println(entry1.getKey() + " => " + entry1.getValue());
            }
        }
    }

<强>输出

32
walks => 32
name => Chase
pic => http://****/images/walkers/chase.png
dist => 6096.8589024135445

答案 2 :(得分:0)

你的HashMap似乎持有某个类的对象,如下所示:

{walks=32, pic=http://****/images/walkers/chase.png, name=Chase, dist=6096.8589024135445}

识别类,如果getter方法可用于pic,name,dist,则使用它们。

答案 3 :(得分:0)

我认为你只是想知道如何获得与密钥相关的值:

map.get("pic");

答案 4 :(得分:-1)

您需要me.getValue().get("pic")me.getValue().get("name")me.getValue().get("dist")

这假定您使用的是泛型,TreeMap被声明为Map<Integer, HashMap<String, String>>,并且您迭代的Map.Entry被声明为Map.Entry<Integer, HashMap<String, String>>

此外,您可以使用for-each循环更轻松地进行迭代。

Map<Integer, HashMap<String, String>> theTreeMap = new TreeMap<>();

// Populate the map here.

for (Map.Entry<Integer, HashMap<String, String>> me : theTreeMap.entrySet()) {
    System.out.println(me.getValue().get("pic"));
    System.out.println(me.getValue().get("name"));
    System.out.println(me.getValue().get("dist"));
}
相关问题