【发布时间】:2014-12-17 22:59:06
【问题描述】:
我正在尝试在目录中查找文件的重复项。
我对这个块有问题,它以文件地址作为参数:
public void findFiles(ArrayList<File> list){
HashMap<String, File> hmap = new HashMap<String, File>();
hmap.put(list.get(0).getName(), list.get(0));
//System.out.println(hmap);
for(Entry<String, File> entry : hmap.entrySet()){
String key = entry.getKey();
File value = entry.getValue();
// i don't understand what I need to write below
if (hmap.containsKey(key))
{
System.out.println("Duplicate: " + key + " in "+ value.getAbsolutePath());
}
}
}
我应该如何重写我的 if 语句?
System.out.println(hmap);
还有下一个例子:
{File 2.txt=D:\Folder1\Folder1-2\Folder1-2-1\File 2.txt}
{DFolder1.txt=D:\Folder1\Folder1-2\Folder1-3-1\DFolder1.txt}
{File 1.txt=D:\Folder1\Folder1-2\File 1.txt}
{File 1.txt=D:\Folder1\Folder1-3\File 1.txt, File 3.txt=D:\Folder1\Folder1-3\File 3.txt}
{File 3.txt=D:\Folder1\File 3.txt}
我有两个“文件 1.txt”
【问题讨论】:
-
HashMap allows duplicates? 的可能重复项
-
是否可以在目录中创建重复文件?
标签: java arrays if-statement hashmap duplicates