首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java 8 lambda表达式:将满足给定谓词的所有项按顺序分组。分组应以谓词为键,项为值

Java 8 lambda表达式:将满足给定谓词的所有项按顺序分组。分组应以谓词为键,项为值
EN

Stack Overflow用户
提问于 2015-03-27 12:43:04
回答 1查看 920关注 0票数 1

以下是样本输入数据和预期输出。我想在输入列表上进行单次迭代来执行此操作。

代码语言:javascript
复制
// input
Predicate<File> sizeP = f -> f.length() > 1_000_000; // size filter
Predicate<File> nameP = f -> f.getName().startsWith("Plot"); // name filter
List<File> fileList;
// output
// files list which satisfy the filter criteria
Map<Predicate<File>,List<File>> rulesToFiles = new HashMap<>();

// Example data
//  1. PlotPresentation.pptx-> 2 MB size      
//  2. Gen.docx-> 500 KB size
//  3. Group.docx-> 1.5 MB size
//  4. PlotDetails.docx-> 850 KB size
//  5. PlotDiagram.docx-> 5 MB size

// my map should have
Map<Predicate<File>,List<File>> rulesToFiles = new HashMap<>();
// [size-predicate]-> [PlotPresentation.pptx,Group.docx,PlotDiagram.docx]
// [name-predicate]-> [PlotPresentation.pptx,PlotDetails.docx,PlotDiagram.docx]
EN

回答 1

Stack Overflow用户

发布于 2015-03-27 18:21:26

为了将有用的键与谓词相关联,我们可以使用Map

代码语言:javascript
复制
Map<String, Predicate<File>> pred=new TreeMap<>();
pred.put("size", f -> f.length() > 1_000_000);
pred.put("name", f -> f.getName().startsWith("Plot"));

然后我们可以像这样处理它们:

代码语言:javascript
复制
Map<String,List<File>> rulesToFiles = 
    fileList.stream().collect(Collectors.groupingBy(f->
        pred.entrySet().stream().filter(e->e.getValue().test(f))
            .map(Map.Entry::getKey).collect(Collectors.joining("+"))
    ));

这会导致

代码语言:javascript
复制
 => [Gen.docx]
size => [Group.docx]
name => [PlotDetails.docx]
name+size => [PlotPresentation.pptx, PlotDiagram.docx]

这与您的问题中的要求不完全相同,但非常有用。也许你可以接受这一点。

但是,如果这不能满足您的要求,您可以对Map应用后处理

代码语言:javascript
复制
if(rulesToFiles.getClass()!=HashMap.class)// ensure mutable Map
    rulesToFiles=new HashMap<>(rulesToFiles);

rulesToFiles.remove(""); // remove the none-matching items
List<File> nameAndSize = rulesToFiles.remove("name+size");// remove&check for common items
if(nameAndSize!=null) {// merge them
    BinaryOperator<List<File>> merger=(a,b)->
        Stream.concat(a.stream(), b.stream()).collect(Collectors.toList());
    rulesToFiles.merge("size", nameAndSize, merger);
    rulesToFiles.merge("name", nameAndSize, merger);
}

结果:

代码语言:javascript
复制
size => [Group.docx, PlotPresentation.pptx, PlotDiagram.docx]
name => [PlotDetails.docx, PlotPresentation.pptx, PlotDiagram.docx]

更新:

我仔细考虑了一下,这里有一个根据请求生成Map的解决方案,首先由Stream操作生成,因此不需要额外的操作。根据第一个解决方案的Map<String, Predicate<File>> pred,使用:

代码语言:javascript
复制
Map<String,List<File>> rulesToFiles = fileList.stream()
    .flatMap(f -> pred.entrySet().stream().filter(e->e.getValue().test(f))
        .map(e->new AbstractMap.SimpleEntry<>(e.getKey(), f)))
    .collect(Collectors.groupingBy(Map.Entry::getKey,
        Collectors.mapping(Map.Entry::getValue, Collectors.toList())));

结果:

代码语言:javascript
复制
size => [PlotPresentation.pptx, Group.docx, PlotDiagram.docx]
name => [PlotPresentation.pptx, PlotDetails.docx, PlotDiagram.docx]
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29293453

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档