Java 地图上的流过滤器和分组列表
我正在制作一个过滤器,允许我生成一个地图,其值是文件列表,为此,我尝试使用流。 我有以下文件列表示例:Java 地图上的流过滤器和分组列表,java,java-8,functional-programming,java-stream,Java,Java 8,Functional Programming,Java Stream,我正在制作一个过滤器,允许我生成一个地图,其值是文件列表,为此,我尝试使用流。 我有以下文件列表示例: CLA_文件名 CLA_文件名 CLA_文件名 CM_文件名 CM_文件名 CM_文件名 SP_文件名 SP_文件名 CON_文件名 CON_文件名 然后,我需要获取文件列表,并将它们带到一个映射,该映射的键将一个列表中的所有类型的文件分组 Map>:所以这将是预期的结果 "CLA_", List <File> 3 CLA Files "CM_
- CLA_文件名
- CLA_文件名
- CLA_文件名
- CM_文件名
- CM_文件名
- CM_文件名
- SP_文件名
- SP_文件名
- CON_文件名
- CON_文件名
"CLA_", List <File> 3 CLA Files
"CM_", List <File> 3 CM Files
"SP_", List <File> 2 SP Files
"CON_", List <File> 2 CON Files
“CLA”,列出3个CLA文件
“CM_”,列出3个CM文件
“SP_”,列出2个SP文件
“CON”,列出2个CON文件
我有下面的代码,它只为我分组了一种文件类型,CLA。你能告诉我如何在地图上过滤和加载这些文件吗
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());
}
List csvList=getFiles();
函数filterCLA=新函数(){
@凌驾
公共字符串应用(文件){
返回String.valueOf(file.getName().startsWith(“CLA_”);
}
};
Map Map=csvList.stream()
.collect(groupingBy(f->filterCLA.apply(f),toCollection(ArrayList::new));
对于(Map.Entry:Map.entrySet()){
System.out.println(entry.getKey()+“-”+entry.getValue());
}
此代码仅使用
CLA\uu
键将CLA文件保存在地图中的列表中。这是正确的,但我需要添加其余的文件。试试这个。任何不以类似XXX\ucode>开头的文件名都将映射到类型为UnknownType
List<File> csvList = List.of(
new File("CLA_FileName"),
new File("CLA_FileName"),
new File("CLA_FileName"),
new File("CM_FileName"),
new File("CM_FileName"),
new File("CM_FileName"),
new File("SP_FileName"),
new File("SP_FileName"),
new File("CON_FileName"),
new File("CON_FileName"),
new File("BadFileName1"),
new File("BadFileName2"));
// get the prefix (e.g. CM_)
Function<File, String> getPrefix = f-> {
String name = f.getName();
// location of delimiter
int index = name.indexOf('_');
if (index < 0) {
return "UNKNOWN_TYPE";
}
return name.substring(0,index+1);
};
// create the map
Map<String, List<File>> map = csvList.stream()
.collect(Collectors.groupingBy(getPrefix));
//print the map
map.entrySet().forEach(System.out::println);
最后,我找到了一个过滤和分组的解决方案。这是代码。
谢谢大家的帮助
List<Predicate<File>> allPredicates = new ArrayList<Predicate<File>>();
allPredicates.add(str -> str.getName().startsWith("CM"));
allPredicates.add(str -> str.getName().startsWith("CLA"));
allPredicates.add(str -> str.getName().startsWith("IDP"));
allPredicates.add(str -> str.getName().startsWith("SP"));
allPredicates.add(str -> str.getName().startsWith("CON"));
return Arrays.asList(csvFiles)
.stream()
.filter(allPredicates.stream().reduce(f->true, Predicate::or))
.collect(Collectors.groupingBy(f-> f.getName().substring(0,f.getName().indexOf("_"))));
List allPredicates=new ArrayList();
add(str->str.getName().startsWith(“CM”);
add(str->str.getName().startsWith(“CLA”);
add(str->str.getName().startsWith(“IDP”));
add(str->str.getName().startsWith(“SP”);
add(str->str.getName().startsWith(“CON”);
返回数组.asList(csvFiles)
.stream()
.filter(allPredicates.stream().reduce(f->true,谓词::or))
.collect(Collectors.groupingBy(f->f.getName().substring(0,f.getName().indexOf(“”)));
使用indexOf
和char
一起使用,就像在name.indexOf(“')
中一样。groupingBy
不需要下游的来收集ArrayList
的函数
限定符可以直接用作分类器
,因此Map=csvList.stream().collect(Collectors.groupingBy(getPrefix))代码>当\uuuu
丢失或需要新前缀时会发生什么情况?您的问题表明您希望将\uu
作为密钥的一部分。
List<Predicate<File>> allPredicates = new ArrayList<Predicate<File>>();
allPredicates.add(str -> str.getName().startsWith("CM"));
allPredicates.add(str -> str.getName().startsWith("CLA"));
allPredicates.add(str -> str.getName().startsWith("IDP"));
allPredicates.add(str -> str.getName().startsWith("SP"));
allPredicates.add(str -> str.getName().startsWith("CON"));
return Arrays.asList(csvFiles)
.stream()
.filter(allPredicates.stream().reduce(f->true, Predicate::or))
.collect(Collectors.groupingBy(f-> f.getName().substring(0,f.getName().indexOf("_"))));