如何在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
最满意答案
我用
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 aUILabel
.
相关问答
更多-
视频的持续时间(OpenCV)(Duration of a video (OpenCV))[2022-08-11]
首先,在尝试获取/设置某些属性之前,从相机/视频中抓取至少一帧。 有时它可以在没有这样做的情况下正常工作,但更好的方法是做到 如果它无法解决您的问题,您可以尝试使用不同的属性: 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 ...
-
Android视频的持续时间(duration of a video on Android)[2020-01-27]
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与UIImagepicker(AVCam vs UIImagepicker)[2023-03-03]
AVCam只是您如何使用AVFoundation来生成您自己的捕捉功能的一个例子。 AVCam演示了如何使用AV Foundation捕捉API来录制电影和拍摄静止图像。 有一个用于录制电影的录制按钮,一个用于在前后摄像头之间切换的摄像头按钮(在支持的设备上),以及一个用于拍摄静止图像的静止按钮。 下一个 所以AVFoundation和ImagePicker之间的区别: 图像选取器就像是一个可定制的黑盒子,除了执行代表以从相机或相机胶卷获取拍摄图像之外,您不需要做任何其他操作。 因此,它非常易于使用,如果你 ... -
确保在完成后调用[AVAssetWriter finishWritingWithCompletionHandler:] 。 您应该使用您收到的第一个缓冲区的显示时间戳调用startSessionAtSourceTime 。 你可以删除[self.assetWriter endSessionAtSourceTime:kCMTimeZero]; 。 Make sure to call [AVAssetWriter finishWritingWithCompletionHandler:] when you've ...
-
捕获视频缓冲实例(Capturing of video buffering instances)[2023-04-14]
对于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 ...