Java 8 lambda expression: Group all items in seque

2019-06-03 06:28发布

问题:

Here is the sample input data and expected output. I want perform this operation with single iteration on the input list.

// 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]

回答1:

In order to associate useful keys with predicates, we may use a Map:

Map<String, Predicate<File>> pred=new TreeMap<>();
pred.put("size", f -> f.length() > 1_000_000);
pred.put("name", f -> f.getName().startsWith("Plot"));

Then we can process them like this:

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("+"))
    ));

this results in

 => [Gen.docx]
size => [Group.docx]
name => [PlotDetails.docx]
name+size => [PlotPresentation.pptx, PlotDiagram.docx]

which isn’t exactly as requested in your question but quite useful. Maybe you can live with that.

But if this doesn’t satisfy you, you may apply a post-processing to the Map:

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);
}

result:

size => [Group.docx, PlotPresentation.pptx, PlotDiagram.docx]
name => [PlotDetails.docx, PlotPresentation.pptx, PlotDiagram.docx]

Update:

I thought it over and here’s a solution to produce a Map as requested, right by the Stream operation in the first place, so no additional operation is required. Based on the Map<String, Predicate<File>> pred of the first solution, use:

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())));

Result:

size => [PlotPresentation.pptx, Group.docx, PlotDiagram.docx]
name => [PlotPresentation.pptx, PlotDetails.docx, PlotDiagram.docx]