How to fix java lambda filter(missing return state

2020-03-26 07:39发布

How to solve java lambda filter future collection?

I got a future collection, And I want to filter out the false result returned in the collection, but using lambda to report (Missing return statement), I want to get a collection looks like List<Map<String, Object>>. What should I do to achieve filtering?

List<Future<Map<String, Object>>> future = 
    childIds.getChildOrder()
            .stream()
            .map(i -> service.submit(new some(i)))
            .collect(Collectors.toList());

            future.stream().filter(i -> {
                try {
                    i.get().get("success").equals(Boolean.FALSE);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }).findAny().get().get();

enter image description here

The Map<String, Object> structure looks like this {"success":"false", "msg":"I got error"}

标签: java lambda
1条回答
乱世女痞
2楼-- · 2020-03-26 08:09

You must have return statements in all execution paths:

future.stream().filter(i -> {
    try {
        return i.get().get("success").equals(Boolean.FALSE);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    return false; // depending on what you wish to return in case of exception
}).findAny().get().get();
查看更多
登录 后发表回答