Is there elegant way to fork and join Java streams? - Stack Overflow

I have quite simple task: there is a List<Map> exposed from unsorted YAMLs and JSONs. I need to f

I have quite simple task: there is a List<Map> exposed from unsorted YAMLs and JSONs. I need to filter the list to find some particular data structures and extract some part of them -- four different filter conditions and extracting of correspondent parts.

My idea was to use streams but they don't support forking or multi-filtering with following split mapping and then joining. I solved the task by mapping the following function to the list, but I don't like its imperative style in functional context:

private Stream<String> getModelNames(Map<String, Map<String, Object>> expression) {
    if (expression.containsKey("keyA")) {
        return Stream.of((String) expression.get("keyA").get("model_name"));
    } else if (expression.containsKey("keyB")) {
        return Stream.of((String) expression.get("keyB").get("model_name"));
    } else if (expression.containsKey("keyC")) {
        return Stream.of((String) expression.get("keyC").get("model_name"));
    } else if (expression.containsKey("keyD")) {
        return ((List)expression.get("keyD").get("cases")).stream()
                .map(cases -> ((List)cases).get(1));
    } else {
        return Stream.of();
    }
}

List<String> modelNames = ruleRepository
       .getSomeData()
       .stream()
       .map(expression -> (Map)yaml.load(expression))
       .map(expression -> getModelNames(expression))
       .flatMap(s -> s)
       .toList();

The question is there more elegant/functional solution?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744275735a4566321.html

相关推荐

  • Is there elegant way to fork and join Java streams? - Stack Overflow

    I have quite simple task: there is a List<Map> exposed from unsorted YAMLs and JSONs. I need to f

    7天前
    60

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信