How to iterate over a nested HashMap

To iterate over a nested HashMap ie a Hashmap has HashMap in it as value to iterate over such HashMap use below code

public static void iterate(Map<String, Object> map) {
	    for (Map.Entry<String, Object> entry : map.entrySet()) {
	        System.out.println("Key is:"+ entry.getKey());
	        if (entry.getValue() instanceof Map) {
	            System.out.println("Map object found, iterating again");
	            iterate((Map<String, Object>) entry.getValue());
	        } else {
	            System.out.println("found end , value is:"+ entry.getValue());
	        }
	    }
	}


Advertisements
This entry was posted in Java. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s