I have the following data set with Key is String and value as List of values.
I wanted to call a method with key and each value of list as parameters to the method. Iterate for all the keys. I am able to do it with two forEach loops as shown in my example below. I would like know if we can write the same logic using streams and flatMap in Java 8 without forEach inner loop? thanks
Map<String,ArrayList<String>> xhashMap ;
if(xhashMap!=null) {
xhashMap.forEach((k,l)-> {
if(k.equals("ax")){
l.forEach(v->{
method1(v,AA.class);
}
}
if(k.equals("bx")){
l.forEach(v->{
method1(v,BB.class);
}
}
});
}
map.getOrDefault("ax", emptyList()).forEach(v -> method1(v, AA.class))or, if missing key is an exceptional condition, simplymap.get("ax").forEach(...)