2014-12-03 25 views
0

我的代码有这个问题。我创建了存储的关键和MD5哈希文件路径作为值这样一个HashMap:如何使用java中的hashMap获取特定的重复值键

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中其被用于比较实际值keys.The getDuplcate方法检查列表和输出仅重复。请高度赞赏你在这个问题上的帮助。

+0

如果问题是什么'getDuplicate'回报,那不是代码相关? – 2014-12-03 14:59:46

+0

请在您的问题中包含您的'file'方法(在'map.put(file(file),...)')中使用的代码,'modedHash.getKeys'方法和'getDuplicate'方法。 – VGR 2014-12-03 15:01:15

+0

我已编辑帖子。 – emekamba 2014-12-03 15:17:49

回答

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); 
      } 
     } 
    } 
} 
+0

非常感谢你,你已经帮了我很多。你真的是最好的......我爱你......你已经解决了我的问题,我非常感谢你的帮助......谢谢。 – emekamba 2014-12-03 21:02:13