ColumnFamily column = tokens.getColumnFamilies().stream()
.filter(family -> family.getName().equals("this_family"))
.findAny()
.get();
Map<String, String> tokenized = column.getColumns().stream()
.collect(Collectors.toMap(
Column::getQualifier,
Column::getValue
));有没有办法把这两个流合并成一个流?我使用第一个流在我的嵌套列表上进行过滤和查找,并使用第二个流根据流的结果创建映射。我想知道是否有一种方法可以使用单个流来完成这项工作。
像这样的东西
Map<String, String> tokenized = tokens.getColumnFamilies().stream()
.filter(family -> family.getName().equals("this_family"))
.collect(Collectors.toMap(
//
));发布于 2020-08-27 11:17:23
您可以使用flatMap获取嵌套的Stream并展平结构:
Map<String, String> tokenized = tokens.getColumnFamilies().stream()
.filter(family -> family.getName().equals("this_family"))
.limit(1) // equivalent of findAny
.flatMap(cf -> cf.getColumns().stream())
.collect(Collectors.toMap(
Column::getQualifier,
Column::getValue
));发布于 2020-08-27 19:34:15
使用Optional#map()和组合
Map<String, String> tokenized = tokens.getColumnFamilies().stream()
.filter(family -> family.getName().equals("this_family"))
.findAny()
.map(cf -> cf.getColumns().stream()).get()
.collect(Collectors.toMap(Column::getQualifier, Column::getValue));https://stackoverflow.com/questions/63608614
复制相似问题