java中在arraylist中收集数据的更快方法

java中在arraylist中收集数据的更快方法,java,list,java-8,stream,Java,List,Java 8,Stream,我有一个包含许多文件的目录,希望筛选具有特定名称的目录,并将它们保存在文件列表ArrayList中,它以这种方式工作,但需要花费很多时间。有没有办法让这更快 String processingDir = "C:/Users/Ferid/Desktop/20181024"; String CorrId = "00a3d321-171c-484a-ad7c-74e22ffa3625"); Path dirPath = Paths.get(processingDir); ArrayLi

我有一个包含许多文件的目录,希望筛选具有特定名称的目录,并将它们保存在
文件列表
ArrayList中,它以这种方式工作,但需要花费很多时间。有没有办法让这更快

String processingDir = "C:/Users/Ferid/Desktop/20181024";
String CorrId = "00a3d321-171c-484a-ad7c-74e22ffa3625");
Path dirPath = Paths.get(processingDir);       

ArrayList<Path> fileList;

try (Stream<Path> paths = Files.walk(dirPath))
{           
    fileList = paths.filter(t -> (t.getFileName().toString().indexOf("EPX_" + 
    corrId + "_") >= 0)).collect(Collectors.toCollection(ArrayList::new));
}
String processingDir=“C:/Users/Ferid/Desktop/20181024”;
字符串CorrId=“00a3d321-171c-484a-ad7c-74e22ffa3625”);
Path dirPath=Path.get(processingDir);
ArrayList文件列表;
try(Stream path=Files.walk(dirPath))
{           
fileList=paths.filter(t->(t.getFileName().toString().indexOf(“EPX_933;”+
corrId+“”>=0)).collect(collector.toCollection(ArrayList::new));
}

在try条件下遍历目录不会花费太多时间,但是在
文件列表中收集目录会花费太多时间,我不知道到底是哪个操作导致了这种糟糕的性能,或者哪些操作需要改进。(这当然不是完整的代码,只是相关内容)

如果每次扫描文件太慢,您可以在启动时或在文件更改时保存和维护文件索引

您可以使用在程序运行时添加或删除文件时收到通知

这将大大加快查询速度,因为它将完全位于内存中。第一次加载需要相同的时间,但可能是在您最初需要它之前加载背景

e、 g

静态映射路径映射;
公共静态void initPathMap(字符串处理目录)引发IOException{
try(Stream path=Files.walk(path.get(processingDir))){
pathMap=path.collect(Collectors.groupingBy(
p->getCorrId(p.getFileName().toString());
}
pathMap.remove(“”;//删除不带corrId的条目。
}
私有静态字符串getCorrId(字符串文件名){
int start=fileName.indexOf(“EPX_”);
如果(开始<0)
返回“”;
int end=fileName.indexOf(“\ux”,start+4);
如果(结束<0)
返回“”;
返回fileName.substring(开始+4,结束);
}
//后来
字符串corrId=“00a3d321-171c-484a-ad7c-74e22ffa3625”;
List pathList=pathMap.get(corrId);//非常快。

您可以通过编写以下代码使代码更干净,但是,我不希望它更快

List<Path> fileList;

try (Stream<Path> paths = Files.walk(dirPath)) {           
    String find = "EPX_" + corrId + "_"; // only calculate this once
    fileList = paths.filter(t -> t.getFileName().contains(find))
                    .collect(Collectors.toList());
}
列表文件列表;
try(Stream path=Files.walk(dirPath)){
String find=“EPX”+corrId+“\”;//只计算一次
fileList=path.filter(t->t.getFileName().contains(find))
.collect(Collectors.toList());
}

成本是扫描目录中的文件所花费的时间。处理文件名的成本要少得多

使用SSD,或者只扫描已经缓存在内存中的目录,将大大加快速度


测试这一点的一种方法是在干净引导后多次执行该操作(因此不会缓存)。第一次运行所花费的时间会告诉您从磁盘加载数据花费了多少时间。

如果每次扫描文件的速度太慢,您可以在启动时或在文件更改时保存和维护文件索引

您可以使用在程序运行时添加或删除文件时收到通知

这将大大加快查询速度,因为它将完全位于内存中。第一次加载需要相同的时间,但可能是在您最初需要它之前加载背景

e、 g

静态映射路径映射;
公共静态void initPathMap(字符串处理目录)引发IOException{
try(Stream path=Files.walk(path.get(processingDir))){
pathMap=path.collect(Collectors.groupingBy(
p->getCorrId(p.getFileName().toString());
}
pathMap.remove(“”;//删除不带corrId的条目。
}
私有静态字符串getCorrId(字符串文件名){
int start=fileName.indexOf(“EPX_”);
如果(开始<0)
返回“”;
int end=fileName.indexOf(“\ux”,start+4);
如果(结束<0)
返回“”;
返回fileName.substring(开始+4,结束);
}
//后来
字符串corrId=“00a3d321-171c-484a-ad7c-74e22ffa3625”;
List pathList=pathMap.get(corrId);//非常快。

您可以通过编写以下代码使代码更干净,但是,我不希望它更快

List<Path> fileList;

try (Stream<Path> paths = Files.walk(dirPath)) {           
    String find = "EPX_" + corrId + "_"; // only calculate this once
    fileList = paths.filter(t -> t.getFileName().contains(find))
                    .collect(Collectors.toList());
}
列表文件列表;
try(Stream path=Files.walk(dirPath)){
String find=“EPX”+corrId+“\”;//只计算一次
fileList=path.filter(t->t.getFileName().contains(find))
.collect(Collectors.toList());
}

成本是扫描目录中的文件所花费的时间。处理文件名的成本要少得多

使用SSD,或者只扫描已经缓存在内存中的目录,将大大加快速度


测试这一点的一种方法是在干净引导后多次执行该操作(因此不会缓存)。第一次运行所需的时间长度告诉您从磁盘加载数据花费了多少时间。

来自
java.nio.file.Files.walk(Path)
api:

通过遍历文件返回一个路径为惰性填充的流 以给定起始文件为根的树

这就是为什么它给您的印象是“在try条件下遍历目录不会花费太多时间”


实际上,真正的交易主要是在
collect
上完成的,这不是collect的机制故障,因为它速度慢。

来自
java.nio.file.Files.walk(Path)
api:

通过遍历文件返回一个路径为惰性填充的流 以给定起始文件为根的树

这就是为什么它给您的印象是“在try条件下遍历目录不会花费太多时间”

实际上,真正的交易大多是在collect上完成的,而不是collect的机制故障