0

I'm trying to find the duplicates of files in the directory.

I have a problem with this block, that has file's address as an argument:

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());
        }
    }
}    

How should I rewrite my if statement?

System.out.println(hmap);  

And have next examlpes:

    {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}        

I have two "File 1.txt"

O.Solodovnikov
  • 99
  • 2
  • 12

3 Answers3

2

Your map won't have duplicate keys when you iterate over the entries. That's how maps are defined. You have to check for duplicates when you add the files to the map.

if (hmap.containsKey(key)) will always return true, since you just got that key from the map.

You have to iterate over the files in the list :

public void findFiles(ArrayList<File> list){
    HashMap<String, File> hmap = new HashMap<String, File>();
    for (File file : list) {
        if (hmap.containsKey(file.getName()) {
            System.out.println("Duplicate: " + file.getName() + " in "+ hmap.get(file.getName()).getAbsolutePath());
        } else {          
            hmap.put(file.getName(), file);
        }
    }
} 
Eran
  • 387,369
  • 54
  • 702
  • 768
2

You only added the first element of the list to the map and then iterate over the map. But I don't thinnk you need a map for your code, you could use a HashSet. Outline:

  1. Create a HashSet.
  2. Iterate over the list of your files
  3. Check if the file is already in the set (if set.contains(...)), if true then you foud a duplicate. else: add file to the set.
morpheus05
  • 4,772
  • 2
  • 32
  • 47
0

Don't use a map to find duplicates. Use a set. The idea is to loop over every file, check if it's already in the set and add it to the set if it isn't.

public List<File> findDuplicates(List<File> files) {
    Set<File> filesChecked = new HashSet<>();
    List<File> duplicates = new ArrayList<>();

    for (File f : files) {
        if (filesChecked.contains(f)) {
            duplicates.add(f);
        } else {
            filesChecked.add(f);
        }
    }

    return duplicates;
}
dusky
  • 1,133
  • 7
  • 12