admin管理员组文章数量:1401969
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?
本文标签: Is there elegant way to fork and join Java streamsStack Overflow
版权声明:本文标题:Is there elegant way to fork and join Java streams? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744275735a2598403.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论