How to return any value like Map/List from Java 8 foreach method?

Create a final instance of a return value.

Like in case of Map create final Map<String, String> result = new HashMap<>();

List<String> list = Arrays.asList("1", "2", "3", "4", "5");

list.foreach(l -> {
result.put(l, l);
});

Here we can access the result object.
System.out.println(result)

Comments

Popular Posts