首页 \ 问答 \ 如何在java中使用hashMap获取特定的重复值键(How to get a specific duplicate values keys using hashMap in java)

如何在java中使用hashMap获取特定的重复值键(How to get a specific duplicate values keys using hashMap in java)

我的代码有这个问题。 我创建了一个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]

这是我的代码,当我运行它时,它显示如下的空集:

null null null null

  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并仅输出重复项。 请高度赞赏你在这个问题上的帮助。


I have this problem with my code. i created a hashMap that store file path as key and Md5 hash as values like this:

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]

I only wanted it to display the duplicate values and it keys equivalence like this:

  Key:[C:\Users\Mbaegbu\Documents\.jpg]  Value:[f884c30bfad89a285507d4c381700583]
  Key :  [C:\Users\Mbaegbu\Documents\.jpg] Value:[f884c30bfad89a285507d4c381700583]
  Key :  [C:\Users\Mbaegbu\Documents\.jpg]  Value:[f884c30bfad89a285507d4c381700583]

Here is my code, when ever i run it, it displays an null set like this:

null null null null

  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;

}

the modedHash class extends HashMap which is used to compare the actual values keys.The getDuplcate method checks the List and output only duplicates. Please your help in this issue will be highly appreciated.


原文:https://stackoverflow.com/questions/27274612
更新时间:2023-02-17 20:02

最满意答案

我用NSTimer对它进行了整理,当我开始捕获时,我安排了一个计时器,并且在停止捕获时,我重置了计时器。 在计时器操作中,增加持续时间ivar并将其显示为UILabel捕获持续时间。


I sorted it out with a NSTimer, when I start capturing I schedule a timer, and on stop capturing, I reset timer. In timer action, incrementing a duration ivar and showing it as capturing duration in a UILabel.

相关问答

更多
  • 首先,在尝试获取/设置某些属性之前,从相机/视频中抓取至少一帧。 有时它可以在没有这样做的情况下正常工作,但更好的方法是做到 如果它无法解决您的问题,您可以尝试使用不同的属性: cv::VideoCapture camera("some_movie.avi"); cv::Mat img; camera >> img; std::cout << camera.get(CV_CAP_PROP_FPS) << std::endl; std::cout << camera.get(CV_CAP_PROP_FRAME ...
  • 我用NSTimer对它进行了整理,当我开始捕获时,我安排了一个计时器,并且在停止捕获时,我重置了计时器。 在计时器操作中,增加持续时间ivar并将其显示为UILabel捕获持续时间。 I sorted it out with a NSTimer, when I start capturing I schedule a timer, and on stop capturing, I reset timer. In timer action, incrementing a duration ivar and s ...
  • 这适用于我: import AVFoundation import CoreMedia ... if let url = Bundle.main.url(forResource: "small", withExtension: "mp4") { let asset = AVAsset(url: url) let duration = asset.duration let durationTime = CMTimeGetSeconds(durati ...
  • Apple的AVCam代码是进入摄影开发的一个很好的起点。 您要做的是修改视频预览图层的大小。 这可以通过更改videoGravity设置来完成。 以下是方面填充类型查看的示例: [Swift 3] previewView.videoPreviewLayer.videoGravity = AVLayerVideoGravityResizeAspectFill 现在,对于填充矩形的情况,您需要定义图层边界,然后使用AVLayerVideoGravityResize 。 请注意:这不会影响拍摄照片的大小。 ...
  • 函数shell_exec返回字符串中的所有输出。 exec只返回最后一行。 试试这个 &1 | grep Duration | awk '{print $2}' | tr -d ,; "; echo exec($command) ; ?> Function shell_exec returns all the output in a string. exec just returns the last line. Try ...
  • videoView中的getDuration复制: public int getDuration(){ if (isInPlaybackState()) { return mMediaPlayer.getDuration(); } return -1; } 因此,从我所看到的情况来看,如果视频未播放,则无法获得正确的持续时间。 因此,一种可能的解决方案是开始播放,获取持续时间,然后停止并重新开始播放视频。 Copied from getDuration in vid ...
  • 看看这个: 如何获得FFMPEG和PHP视频持续时间 在那个页面中,有一些代码似乎适用于某个用户。 我必须承认我没有测试过,所以完全取决于你: $videofile="/var/video/user_videos/partofvideo.avi"; ob_start(); passthru("/usr/bin/ffmpeg -i \"{$videofile}\" 2>&1"); $duration = ob_get_contents(); ob_end_clean(); $search='/Duratio ...
  • AVCam只是您如何使用AVFoundation来生成您自己的捕捉功能的一个例子。 AVCam演示了如何使用AV Foundation捕捉API来录制电影和拍摄静止图像。 有一个用于录制电影的录制按钮,一个用于在前后摄像头之间切换的摄像头按钮(在支持的设备上),以及一个用于拍摄静止图像的静止按钮。 下一个 所以AVFoundation和ImagePicker之间的区别: 图像选取器就像是一个可定制的黑盒子,除了执行代表以从相机或相机胶卷获取拍摄图像之外,您不需要做任何其他操作。 因此,它非常易于使用,如果你 ...
  • 确保在完成后调用[AVAssetWriter finishWritingWithCompletionHandler:] 。 您应该使用您收到的第一个缓冲区的显示时间戳调用startSessionAtSourceTime 。 你可以删除[self.assetWriter endSessionAtSourceTime:kCMTimeZero]; 。 Make sure to call [AVAssetWriter finishWritingWithCompletionHandler:] when you've ...
  • 对于Apache JMeter,你可以使用这个计算缓冲区的插件: http://www.ubik-ingenierie.com/blog/load-testing-mpeg-dash-jmeter-ubik-videostreaming-plugin/ http://www.ubik-ingenierie.com/blog/ubikloadpack-http-live-streaming-plugin-jmeter-videostreaming-mpegdash/ 它处理HLS,MPEG-DASH和MS S ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)