-2

I've got a TreeMap that stores a HashMap inside of it. I feel like I should be able to find this, but I just can't seem to find it on Google.

I've got a TreeMap with a HashMap stored inside of it, I iterate over it like so:

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

That will print out an output (example line):

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

I'm wondering how to now grab pic, name, dist from this HashMap.

Edit: I'm not understanding where people missed the point. I put a HashMap into the TreeMap. Inside of the TreeMap is a HashMap. I guess I can show you what a HashMap is, but you guys know that already!

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);
user1610719
  • 1,275
  • 2
  • 18
  • 35

5 Answers5

2

All you need is a cast to the TreeMap values to a Map again:

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"));
}
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
1

Need to iterate twice, one for TreeMap and then for 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());
            }
        }
    }

output

32
walks => 32
name => Chase
pic => http://****/images/walkers/chase.png
dist => 6096.8589024135445
Ankur Singhal
  • 26,012
  • 16
  • 82
  • 116
0

Your HashMap seems to be holding an object of some class, which is depicted here:

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

Identify the class, and if getter methods are available for pic, name, dist, then use them.

Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
  • Yea I don't see any getter methods. I'm thinking I'll have to change the object into something else, as it seems the HashMap didn't stay a HashMap – user1610719 Jan 20 '15 at 03:09
0

I think you are just asking how to get the value associated with a key:

map.get("pic");
sprinter
  • 27,148
  • 6
  • 47
  • 78
  • It won't let me use get. I added more details about the makeup of the TreeMap, to show the HashMap I mentioned I was adding to it. – user1610719 Jan 20 '15 at 03:10
-1

You want me.getValue().get("pic"), me.getValue().get("name") and me.getValue().get("dist").

This assumes that you're using generics, that your TreeMap is declared as a Map<Integer, HashMap<String, String>> and that your Map.Entry that you iterate with is declared as a Map.Entry<Integer, HashMap<String, String>>.

Also, you could iterate more easily with a for-each loop.

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"));
}
Dawood ibn Kareem
  • 77,785
  • 15
  • 98
  • 110
  • get isn't available, I added more details! – user1610719 Jan 20 '15 at 03:09
  • What do you mean "get isn't available"? You said it was a `HashMap`. You ARE using generics, right, so that you don't have to cast it? Something like `TreeMap>` or whatever the types are meant to be? – Dawood ibn Kareem Jan 20 '15 at 03:10
  • Check my edit. That being said, I added TreeMap walks_mp = new TreeMap>(); as the constructor and it still doesn't let me use the code you suggested. get isn't there – user1610719 Jan 20 '15 at 03:16
  • What doesn't let you? `HashMap` has a method called `get`. – Dawood ibn Kareem Jan 20 '15 at 03:16
  • It says "Cannot resolve method get()" when I try to use me.getValue().get("dist"). It's likely something else I did, but it isn't there. If I do me.getValue().getClass(), I get class java.util.HashMap – user1610719 Jan 20 '15 at 03:18
  • And `me` is declared as `Map.Entry>` to match the declaration of your `TreeMap`? – Dawood ibn Kareem Jan 20 '15 at 03:19