For example, given a map as shown below:
{
"k1": {
"k2": {
"k3": {
"k4": "v"
}
}
}
}
and a list of fields ["k1","k2","k3"], I need to get a part {"k4": "v"}.
Below is my java7 style code:
Map map1 = new HashMap();
Map map2 = new HashMap();
Map map3 = new HashMap();
Map map4 = new HashMap();
map4.put("k4", "v");
map3.put("k3", map4);
map2.put("k2", map3);
map1.put("k1", map2);
Map map = map1;
System.out.println(map);
List<String> fields = Arrays.asList("k1", "k2", "k3");
for(String field: fields) {
map = (Map) map.get(field);
}
System.out.println(map);
Then how to convert the code above to java 8 stream style?
source
share