java - HashMap of HashMaps traversal -
in java, i'm trying retrieve hashmap<string, object>
has object is: hashmap<string, object>
.
i implemented recursive function returns either hashmap<string, object>
found given key, or null
if key wasn't found.
here function:
public static hashmap<string, object> gethashmap(hashmap<string, object> map, string key) { (map.entry<string, object> entry : map.entryset()) { if (entry.getvalue().getclass().getname() == "java.util.hashmap") { if (entry.getkey() == key) return (hashmap<string, object>) entry.getvalue(); return gethashmap((hashmap<string, object>) entry.getvalue(), key); } } return null; }
it works first item. how traverse hashmap of hashmaps? better approach?
instead of returning value here:
return gethashmap((hashmap<string, object>) entry.getvalue(), key);
you want first check if not null
, , return then. otherwise should continue searching:
hashmap<string, object> result = gethashmap((hashmap<string, object>) entry.getvalue(), key); if (result != null) return result;
Comments
Post a Comment