-2

I have a hashmap used to store the platform details I need to iterate Map<String, Map<String, String>> finalmapWin8 and get the values and keys of mapWin8 values 33,8 and key BrowserType.CHROME, BrowserType.FIREFOX and BrowserType.IE Also i want to get the key of 'finalmapWin8' "WIN8_1" How can i iterate Map<String, Map<String, String>> finalmapWin8

 static Map<String, Map<String, String>> finalmapWin8 = new HashMap<Platform, Map<String, String>>();

    public static final Map<String, String> mapWin8 = new HashMap<String, String>();
    static {
        mapWin8.put(BrowserType.CHROME, "33");
        mapWin8.put(BrowserType.FIREFOX, "33");
        mapWin8.put(BrowserType.IE, "8");
    }   
    static {
        finalmapWin8.put("WIN8_1", mapWin8);      
    }
Psl
  • 3,830
  • 16
  • 46
  • 84

4 Answers4

2

iteration for Map is the same. since your value for your map is also a Map then you just need another iteration to iterate through it.

    for(String s: finalmapWin8.keySet()){
        System.out.println(s + " : ");
        for(Entry<String, String> entry : finalmapWin8.get(s).entrySet()){
           System.out.println(entry);
        }
    }
nafas
  • 5,283
  • 3
  • 29
  • 57
1

You can use a for loop for loop for Map of type Map.Entry<String, Map<String, String>> ... well, that's it.. See the code snippet below -

import java.util.HashMap;
import java.util.Map;

public class Test {

    static Map<String, Map<String, String>> finalmapWin8 = new HashMap<String, Map<String,String>>();
    public static final Map<String, String> mapWin8 = new HashMap<String, String>();

    static {
        mapWin8.put("CHROME", "33");
        mapWin8.put("FIREFOX", "33");
        mapWin8.put("IE", "8");
    }   
    static {
        finalmapWin8.put("WIN8_1", mapWin8);      
    }

    public static void main(String[] args) {


                for(Map.Entry<String, Map<String, String>> entry : finalmapWin8.entrySet()) {
                    System.out.println(entry.getValue());
                }


    }
}
Puneet Pandey
  • 960
  • 2
  • 14
  • 28
0

// get the keysets or outer map and then again run a secound loop for inner map.

for (Map.Entry<String, Map<String, String>> entry : firstMap) {
  system.out.println("entry="+entry );
}

Refer to How to efficiently iterate over each Entry in a Map?

Community
  • 1
  • 1
Sheetal Mohan Sharma
  • 2,908
  • 1
  • 23
  • 24
0

You can use below code for iterating through HashMap

for (Map.Entry<String, Map<String, String>> entry : finalmapWin8.entrySet()) {

           System.out.println("Key:"+entry.getKey());

           for (Map.Entry<String, String> entry1 : mapWin8.entrySet()) {
              System.out.println("Keys in Second:"+ entry1.getKey()+" Values:"+ entry1.getValue());
           }
        }