Fixed Duplicates in HashMap

This commit is contained in:
bly 2023-01-03 13:37:58 +01:00
parent 529322e933
commit 075bd9511f
2 changed files with 26 additions and 23 deletions

View file

@ -20,21 +20,17 @@ public class ThreadedCompare extends Thread {
for (Path file2 : pathsToCompareTo) {
try {
if (sameContent(file1, file2)) {
List<Path> bothPaths = new ArrayList<>();
bothPaths.add(file1);
bothPaths.add(file2);
List<Path> bothList = new ArrayList<>();
bothList.add(file1);
bothList.add(file2);
//here it is trying to add the values in the HashMap
if (Main.fileMap.containsKey(getMD5Sum(file1.toFile()))) {
if (!Main.fileMap.get(getMD5Sum(file1.toFile())).contains(file1)) {
Main.fileMap.get(getMD5Sum(file1.toFile())).add(file1);
}
if (!Main.fileMap.get(getMD5Sum(file1.toFile())).contains(file2)) {
Main.fileMap.get(getMD5Sum(file1.toFile())).add(file2);
}
} else {
Main.fileMap.put(getMD5Sum(file1.toFile()), bothPaths);
}
Main.fileMap.putIfAbsent(getMD5Sum(file1.toFile()), bothList);
Main.fileMap.get(getMD5Sum(file1.toFile())).removeAll(bothList);
Main.fileMap.get(getMD5Sum(file1.toFile())).addAll(bothList);
}
} catch (IOException e) {
throw new RuntimeException(e);