Q:

Java lambda and HashMap

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
    ));

A:

First populate a map with the key-value mappings where keys are from the if check and values are handler implementations such as foo, bar etc. Then use that map in your lambda expressions like so,
Map<String, Handler> nameToHandler = new HashMap<>();
nameToHandler.put("Foo", new FooHandler());
nameToHandler.put("Bar", new BarHandler());

Map<Boolean, List<ToDo>> result = data.stream()
    .collect(Collectors.partitioningBy(task -> 
        nameToHandler.get(task.getTask()).handleTask(task)));

Notice that the objects with the same key are collected into a container using a downstream collector.
Assumptions: FooHandler and BarHandler implements the Handler interface. Ideally this map should be a static one and would probably be initialized in a static initializer block. Here's how it looks in practice.
private static final Map<String, Handler> nameToHandler = new HashMap<>();

static {
    nameToHandler.put("Foo", new FooHandler());
    nameToHandler.put("Bar", new BarHandler());
}

Incidentially notice that Java static initializers are thread safe. However, if you want to ensure that the code is executed exactly once you need to make sure that the class is only loaded by a single class-loader. Static initialization is performed once per class-loader.

