在一长串文件中查找3个最近修改过的文件 [英] Finding the 3 most recently modified files in a long list of files
问题描述
我有一个文件列表,我想排序并提取最后修改的前三个。
I have a file list which I want to sort and extract the top 3 last modified.
约束:由于下游应用程序的兼容性问题,我无法使用Java 7
Constraint: I cannot use Java 7 due to compatibility issues on downstream apps
File[] files = directory.listFiles();
Arrays.sort(files, new Comparator<File>(){
public int compare(File f1, File f2)
{
return Long.valueOf(f1.lastModified()).compareTo(f2.lastModified());
} });
解决方案2
Solution 2
public static void sortFilesDesc(File[] files) {
Arrays.sort(files, new Comparator() {
public int compare(Object o1, Object o2) {
if ((File)o1).lastModified().compareTo((File)o2).lastModified()) {
return -1;
} else if (((File) o1).lastModified() < ((File) o2).lastModified()) {
return +1;
} else {
return 0;
}
}
});
}
问题
上述两种解决方案需要更多时间来执行&记忆。我的文件列表包含大约300个tar文件,每个文件大小为200MB。所以它消耗更多的时间和记忆。
Problem
The above two solution takes more time to execute & memory. My file list consists of some 300 tar files with 200MB size each. so it is consuming more time & memory.
有没有办法有效地处理这个?
Is there is any way to efficiently handle this?
每个比较操作都使用一个文件对象高内存是否有任何方法可以释放内存并有效地处理它?</ p>
Every compare operation uses a file object which is of high memory is there is any way to release the memory and handle this effectively?
推荐答案
你可以更快地完成它。
You can do it much faster.
Arrays.sort(...)使用快速排序,它采用 ~n * ln(n)操作。
Arrays.sort(...) uses "quick sort", which takes ~ n * ln(n) operations.
此示例仅通过整个数组进行一次迭代,即 ~n 操作。
This example takes only one iteration trough the whole array, which is ~ n operations.
public static void sortFilesDesc(File[] files) {
File firstMostRecent = null;
File secondMostRecent = null;
File thirdMostRecent = null;
for (File file : files) {
if ((firstMostRecent == null)
|| (firstMostRecent.lastModified() < file.lastModified())) {
thirdMostRecent = secondMostRecent;
secondMostRecent = firstMostRecent;
firstMostRecent = file;
} else if ((secondMostRecent == null)
|| (secondMostRecent.lastModified() < file.lastModified())) {
thirdMostRecent = secondMostRecent;
secondMostRecent = file;
} else if ((thirdMostRecent == null)
|| (thirdMostRecent.lastModified() < file.lastModified())) {
thirdMostRecent = file;
}
}
}
关于少量文件你不会有太大差异,但即使对于数十个文件,差异也会很大,对于更大的数字 - 戏剧性。
On small numbers of files you won't see much difference, but even for tens of files the difference will be significant, for bigger numbers - dramatic.
检查算法的代码(请放入一个正确的文件结构):
The code to check the algorithm (please put in a correct files structure):
package com.hk.basicjava.clasload.tests2;
import java.io.File;
import java.util.Date;
class MyFile extends File {
private long time = 0;
public MyFile(String name, long timeMills) {
super(name);
time = timeMills;
}
@Override
public long lastModified() {
return time;
}
}
public class Files {
/**
* @param args
*/
public static void main(String[] args) {
File[] files = new File[5];
files[0] = new MyFile("File1", new Date(2013,1,15, 7,0).getTime());
files[1] = new MyFile("File2", new Date(2013,1,15, 7,40).getTime());
files[2] = new MyFile("File3", new Date(2013,1,15, 5,0).getTime());
files[3] = new MyFile("File4", new Date(2013,1,15, 10,0).getTime());
files[4] = new MyFile("File5", new Date(2013,1,15, 4,0).getTime());
sortFilesDesc(files);
}
public static void sortFilesDesc(File[] files) {
File firstMostRecent = null;
File secondMostRecent = null;
File thirdMostRecent = null;
for (File file : files) {
if ((firstMostRecent == null)
|| (firstMostRecent.lastModified() < file.lastModified())) {
thirdMostRecent = secondMostRecent;
secondMostRecent = firstMostRecent;
firstMostRecent = file;
} else if ((secondMostRecent == null)
|| (secondMostRecent.lastModified() < file.lastModified())) {
thirdMostRecent = secondMostRecent;
secondMostRecent = file;
} else if ((thirdMostRecent == null)
|| (thirdMostRecent.lastModified() < file.lastModified())) {
thirdMostRecent = file;
}
}
System.out.println("firstMostRecent : " + firstMostRecent.getName());
System.out.println("secondMostRecent : " + secondMostRecent.getName());
System.out.println("thirdMostRecent : " + thirdMostRecent.getName());
}
}
这篇关于在一长串文件中查找3个最近修改过的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!