I am making a filter that allows me to generate a Map whose values are lists of files, for this, I have tried to use Streams. I have the following example of Files list:
- CLA_FileName
- CLA_FileName
- CLA_FileName
- CM_FileName
- CM_FileName
- CM_FileName
- SP_FileName
- SP_FileName
- CON_FileName
- CON_FileName
Then I need to take that list of files and take them to a map whose key groups all the files of a type within a single list;
Map <String, List < File>>: So this would be the expected result.
"CLA_", List <File> 3 CLA Files
"CM_", List <File> 3 CM Files
"SP_", List <File> 2 SP Files
"CON_", List <File> 2 CON Files
I have the following code, which is only grouping one file type for me, CLA. Please can you give me an idea of how to filter and load these files on the Map?
List<File> csvList = getFiles();
Function<File, String> filterCLA = new Function<File, String>() {
@Override
public String apply(File file) {
return String.valueOf(file.getName().startsWith("CLA_"));
}
};
Map<String, List<File>> map = csvList.stream()
.collect(groupingBy(f-> filterCLA.apply(f), toCollection(ArrayList::new)));
for(Map.Entry<String, List<File>> entry: map.entrySet()){
System.out.println(entry.getKey()+" - "+ entry.getValue());
}
This code is only saving the CLA files in a list within the map, with CLA_ key. This is correct but I need to add the rest of the files.