I am just started to get familiar with lambdas. I can solve my problem with an easier way.. but i think this version of my solution would be more impressive. Let me explain...
I have a List<Object>
.. i would like to iterate through all the objects inside the list, and i would like to call a method depend on one of the Object's field. The method returns a boolean.
And this is the main part of my problem. I would like to collect them into a Map<Boolean, Object>
so if the method runned successfully i need a K:true,V:Object in my beautiful Map if not runned successfully i want a K:false,V:Object in the Map... and so on.
Here is my code: (Where 'data' is my List<Object>
)
Map<Boolean, ToDo> result = data.stream().collect(Collectors.toMap(
task -> {
if (task.getTask().equalsIgnoreCase("Foo")) {
foo.handle(task);
} else if (task.getTask().equalsIgnoreCase("Boo")) {
boo.handle(task);
}
}, task -> task
));