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]
In order to associate useful keys with predicates, we may use a
Map
:Then we can process them like this:
this results in
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
:result:
Update:
I thought it over and here’s a solution to produce a
Map
as requested, right by theStream
operation in the first place, so no additional operation is required. Based on theMap<String, Predicate<File>> pred
of the first solution, use:Result: