{
"k1": {
" k2": {
"k3": {
"k4": "v"
}
}
}
}
and the field list [“k1”, “k2”, “k3”], I need to retrieve the part {“k4”: “v”}.
The following is my java7 style code: p>
// Ignore the map building 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 ); //=> {k1={k2={k3={k4=v}}}}
// Code to be transformed to java8 style
Listfields = Arrays.asList("k1", "k2", "k3");
for(String field: fields) {
map = (Map) map.get(field);
}
System.out.println(map); //=> {k4=v}
So how to convert the above code to java 8 streaming?
But for completeness, you can do this in the following ways:
map = (Map) fields.stream()
.map(key -> m -> ((Map)m).get(key))
.reduce(Function.identity(), Function::andThen ).apply(map);
This will convert each key into a function capable of doing a map lookup on that key, and then combine them into a single function that is applied to your map. Postpone the operation until This point is necessary because functions are not allowed to modify local variables.
The map operation can also be combined with the reduce operation, which allows the omission of explicit types (
map = (Map)fields.parallelStream()
.reduce(Function.identity(), (f, key)->f.andThen(m->((Map)m) .get(key)), Function::andThen)
.apply(map);
Maybe you realize now that this is a simple for loop for a more suitable task.
For example, give the following map:
{
"k1": {
"k2": {
"k3": {
"k4": "v"
}
}
}
}
< p>and the field list [“k1”, “k2”, “k3”], I need to retrieve the part {“k4”: “v”}.
The following is my java7 style code:< /p>
// Ignore the map building code.
Map map1 = new HashMap();
Map map2 = new H ashMap();
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;< br />System.out.println(map); //=> {k1={k2={k3={k4=v}}}}
// Code to be transformed to java8 style
Listfields = Arrays.asList("k1", "k2", "k3");
for(String field: fields) {
map = (Map) map. get(field);
}
System.out.println(map); //=> {k4=v}
So how to convert the above code to java 8 stream Style?
I don’t think there is any benefit in turning it into a functional style; the loop is good and expresses exactly what you are doing.
p>
But for completeness, you can do this in the following ways:
map = (Map)fields.stream()
.map(key -> m -> ((Map)m).get(key))
.reduce(Function.identity(), Function::andThen).apply(map);
< p> This will convert each key into a function capable of doing a map lookup for that key, and then combine them into a single function that is applied to your map. It is necessary to defer the operation to this point because the function is not allowed to modify the local Variables.
The map operation can also be combined with the reduce operation, which allows the omission of explicit types (< Function>):
map = (Map) fields.parallelStream()
.reduce(Function.identity(), (f, key)->f.andThen(m->((Map)m).get(key)), Function::andThen)
.apply(map);
Maybe you realize now that this is a simple for loop is more suitable for the task.