I am iterating a map like below using Java 8 with forEach
Map<Integer,String> testMap = new HashMap<>();
testMap.put(1, "Atul");
testMap.put(2, "Sudeep");
testMap.put(3, "Mayur");
testMap.put(4, "Suso");
testMap.entrySet().forEach( (K)-> {
System.out.println("Key ="+K.getKey()+" Value = "+K.getValue());
System.out.println("Some more processing ....");
}
);
My question is :
1) How do we extract a method out of forEach while processing in a map?
2)That is, the portion of code inside forEach should be wrapped inside method:
System.out.println("Key ="+K.getKey()+" Value = "+K.getValue());
System.out.println("Some more processing ....");
3) I understand the forEach method in this case expects a Consumer Functional Interface` which has below signature -
void accept(T t);
4) So what I want is something like this :
//declare a consumer object
Consumer<Map.Entry<Integer,String>> processMap = null;
// and pass it to ForEach
testMap.entrySet().forEach(processMap);
5) Can we achieve this?