我的代码存在此问题。我创建了一个hashMap,它将文件路径存储为密钥,将Md5哈希存储为如下值:
Key : [C:\Users\Mbaegbu\Documents\.jpg] Valu : [36305b66726e9d3d8d9bbff2ec07e63b]
Key : [C:\Users\Mbaegbu\Documents\.jpg] Value : [88434b1b9794feacb373048fccd901c1]
Key : [C:\Users\Mbaegbu\Documents\.jpg] Value : [3743436e13b95c2e9ff46dbd8516ea48]
Key : [C:\Users\Mbaegbu\Documents\.avi] Value : [416c2fe27204ce3a78abfb18963c4eb7]
Key : [C:\Users\Mbaegbu\Documents\.jpg] Value : [8fd65bee3e7d26328e978cf2925c3625]
Key : [C:\Users\Mbaegbu\Documents\.avi] Value : [37ba22d609b4e384c00c35ca70a1837d]
我只希望它显示重复的值,它的键等价如下:
Key:[C:\Users\Mbaegbu\Documents\.jpg] Value:[f884c30bfad89a285507d4c381700583]
Key : [C:\Users\Mbaegbu\Documents\.jpg] Value:[f884c30bfad89a285507d4c381700583]
Key : [C:\Users\Mbaegbu\Documents\.jpg] Value:[f884c30bfad89a285507d4c381700583]
这是我的代码,当我运行它时,它会显示如下的空集:
空 空值 空值 空
Map<Set<String>, List<String>> map = new HashMap<>();
try {
for(File file : f.listFiles()){
//System.out.println((hashed(file)));
map.put(file(file),(hashed(file)));
}
} catch (IOException ex) {
Logger.getLogger(DelDuplicate.class.getName()).log(Level.SEVERE, null, ex);
}
Map<Set<String>,List<String>> map2 = new HashMap<>();
map2.putAll(map);
for (Map.Entry<Set<String>, List<String> > entry : map2.entrySet()) {
//System.out.println(" Key : " + entry.getKey()
//+ " Value : " + entry.getValue());
modedHash mode = new modedHash();
System.out.println(mode.getKeys(getDuplicate(entry.getValue())));
}
}
public class modedHash extends HashMap{
public List<Object> getKeys(Object value){
List<Object> keys = null;
if(containsValue(value)){
keys = new ArrayList<>();
Set<String> keyset = keySet();
for (Object key : keyset){
if(get(key).equals(value)){
keys.add(key);
}
}
}
return keys;
}
public List<String> hashed(File file) throws IOException{
List<String> list = new ArrayList<>();
String hash;
if(file.exists() && file != (null)){
//for(File f : file.listFiles()){
hash = MD5.asHex(MD5.getHash(file));
list.add(hash);
//}
}
return list;
}
public Set<String> file (File f){
Set<String> list = new HashSet<>();
if(f.exists() && f != (null)){
//for(File file : f.listFiles()){
list.add(f.getPath());
// }
}
return list;
}
public static <T> List getDuplicate(Collection<T> check){
final List<T> duplicates = new ArrayList<>();
Set<T> uniques = new HashSet<T>(){
public boolean add(T e){
if(contains(e)){
duplicates.add(e);
}
return super.add(e);
}
};
for(T t : check)
uniques.add(t);
return duplicates;
}
modedHash类扩展HashMap,用于比较实际值键.getDuplcate方法检查List并仅输出重复项。请高度赞赏你在这个问题上的帮助。
答案 0 :(得分:0)
您的目标似乎是查找具有重复MD5哈希值的文件。由于哈希是文件的预期唯一标识符,因此您应该将其用作密钥:
public void findDuplicateFiles(File[] files) {
// In Java 8, the following loop can be replaced by:
//Map<String, List<File>> filesByHash =
// Stream.of(files).filter(File::isFile).collect(
// Collectors.groupingBy(file -> MD5.asHex(MD5.getHash(file))));
Map<String, List<File>> filesByHash = new HashMap<>();
for (File file : files) {
if (!file.isFile()) {
continue;
}
String hash = MD5.asHex(MD5.getHash(file));
List<File> filesForHash = filesByHash.get(hash);
if (filesForHash == null) {
filesByHash.put(hash, filesForHash = new ArrayList<File>());
}
filesForHash.add(file);
}
for (Map.Entry<String, List<File>> entry : filesByHash.entrySet()) {
List<File> filesForHash = entry.getValue();
if (filesForHash.size() > 1) {
String hash = entry.getKey();
System.out.printf("%,d files have hash %s:%n",
filesForHash.size(), hash);
for (File file : filesForHash) {
System.out.println(" " + file);
}
}
}
}