replaced Algorythm and made it way faster

This commit is contained in:
BlyDoesCoding 2023-03-24 21:04:32 +01:00
parent a0d2e5b90f
commit c721ba3486
3 changed files with 54 additions and 43 deletions

15
.idea/workspace.xml generated
View file

@ -10,7 +10,9 @@
</component>
<component name="ChangeListManager">
<list default="true" id="9b57ac51-c870-474b-9dfd-64a5fc490635" name="Changes" comment="Fix MD">
<change beforePath="$PROJECT_DIR$/README.md" beforeDir="false" afterPath="$PROJECT_DIR$/README.md" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/Main.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/ThreadedCompare.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/ThreadedCompare.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -72,7 +74,7 @@
<configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Main" />
<module name="Sherly" />
<option name="PROGRAM_PARAMETERS" value="-f $USER_HOME$/Pictures/ -p -h" />
<option name="PROGRAM_PARAMETERS" value="-f $USER_HOME$/.local/share/Steam -p" />
<method v="2">
<option name="Make" enabled="true" />
</method>
@ -260,7 +262,14 @@
<option name="project" value="LOCAL" />
<updated>1679508433400</updated>
</task>
<option name="localTasksCounter" value="25" />
<task id="LOCAL-00025" summary="Fix MD">
<created>1679508468115</created>
<option name="number" value="00025" />
<option name="presentableId" value="LOCAL-00025" />
<option name="project" value="LOCAL" />
<updated>1679508468115</updated>
</task>
<option name="localTasksCounter" value="26" />
<servers />
</component>
<component name="Vcs.Log.Tabs.Properties">

View file

@ -121,7 +121,8 @@ public class Main {
//sectionedList gives the thread their Assigned Part of Files and allFiles are all the Files
ThreadedCompare threadedCompare = new ThreadedCompare(sectionedList, allFiles);
ThreadedCompare threadedCompare = new ThreadedCompare(sectionedList);
threadedCompare.start();
}
@ -136,6 +137,13 @@ public class Main {
}
}
ArrayList toRemove = new ArrayList<String>();
for (String md5: fileMap.keySet()) {
if (Main.fileMap.get(md5).size() == 1) {
toRemove.add(md5);
}
}
fileMap.keySet().removeAll(toRemove);
//now everything is finished and the Filemap (hashmap with all Dups) can be printed out in a nice view
//System.out.println(fileMap);

View file

@ -4,55 +4,49 @@ import java.nio.file.Path;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class ThreadedCompare extends Thread {
private final List<Path> pathsToCompareTo;
private final List<Path> pathsToBeCompared;
public ThreadedCompare (List<Path> pathsToBeCompared, List<Path> pathsToCompareTo) {
this.pathsToBeCompared = pathsToBeCompared;
//private HashMap<String, List<Path>> threadFileMap = new HashMap<>();
public ThreadedCompare (List<Path> pathsToCompareTo) {
this.pathsToCompareTo = pathsToCompareTo;
}
@Override
public void run() {
//Compare every File
for (Path file1 : pathsToBeCompared) {
for (Path file2 : pathsToCompareTo) {
for (Path file : pathsToCompareTo) {
List<Path> fileArray = new ArrayList<>();
assert fileArray != null;
fileArray.add(file);
String MD5;
try {
if (sameContent(file1, file2)) {
List<Path> bothList = new ArrayList<>();
String md5 = getMD5Sum(file1.toFile());
bothList.add(file1);
bothList.add(file2);
//here it is trying to add the values in the HashMap so everything is nice and clear
Main.fileMap.putIfAbsent(md5, bothList);
if (!bothList.isEmpty()) {
Main.fileMap.get(md5).remove(file1);
Main.fileMap.get(md5).remove(file2);
Main.fileMap.get(md5).addAll(bothList);
}
}
MD5 = getMD5Sum(file.toFile());
} catch (IOException e) {
throw new RuntimeException(e);
}
if (Main.fileMap.containsKey(MD5)) {
fileArray.addAll(Main.fileMap.get(MD5));
Main.fileMap.put(MD5, fileArray);
} else {
Main.fileMap.put(MD5, fileArray);
}
//Update the Progress that can be found in The Main Class
Main.progress += 1;
Main.progress++;
}
//Update the thread Completion Counter that can be found in the Main Class
Main.completedThreads += 1;
}
private boolean sameContent(Path file1, Path file2) throws IOException {
if (file1 != file2) {
return Files.mismatch(file1, file2) == -1;
}
return false;
Main.completedThreads++;
}
//this is used to get the MD5 String of one of the files (one of them is just fine since they both have the same value)