去重算法-hash-set

Well, as Bavarious pointed out in a comment, Apple's actual CoreFoundation source is open and available for your perusal too. NSSet is implemented on top of CFSet, whose code is generated (as is that of CFDictionary) from a hash table template, using CFBasicHash to do the work. 

The difference between mutablility and immutability seems to be the matter of a flag in the structure (line 91 of CFBasicHash.h), and from my reading so far just affects calls to functions such as CFBasicHashAddValue; there's a simple check for the mutability. It seems likely, however, that Cobbal is right about the copy/retain behavior between the two (I just haven't read that far yet).

 

保证数据的唯一性。

 

https://stackoverflow.com/questions/5863510/nsset-implementation

转载于:https://www.cnblogs.com/feng9exe/p/9965379.html

文件的 hash 值可以通过 Java 的 MessageDigest 类来计算,具体实现步骤如下: 1. 读取文件内容,可以使用 FileInputStream 类来读取文件; 2. 使用 MessageDigest 类来计算文件内容的 hash 值; 3. 将 hash 值转换为十六进制字符串,可以使用 BigInteger 类来实现; 4. 将每个文件的 hash 值存储到一个 Set 集合中,用于判断文件是否重复。 以下是一个示例代码: ```java import java.io.File; import java.io.FileInputStream; import java.math.BigInteger; import java.security.MessageDigest; import java.util.HashSet; import java.util.Set; public class FileHashDeduplication { public static void main(String[] args) { String directoryPath = "C:/path/to/directory"; Set<String> hashSet = new HashSet<>(); File directory = new File(directoryPath); if (directory.isDirectory()) { File[] files = directory.listFiles(); if (files != null) { for (File file : files) { try { String hash = getFileHash(file); if (!hashSet.contains(hash)) { hashSet.add(hash); // do something with the file System.out.println(file.getPath()); } } catch (Exception e) { e.printStackTrace(); } } } } } private static String getFileHash(File file) throws Exception { MessageDigest messageDigest = MessageDigest.getInstance("MD5"); FileInputStream fileInputStream = new FileInputStream(file); byte[] buffer = new byte[1024]; int len; while ((len = fileInputStream.read(buffer)) != -1) { messageDigest.update(buffer, 0, len); } fileInputStream.close(); BigInteger bigInteger = new BigInteger(1, messageDigest.digest()); return bigInteger.toString(16); } } ``` 在上面的代码中,getFileHash() 方法用于计算文件的 hash 值,使用 MD5 算法来计算,计算出的结果以十六进制字符串的形式返回。getFileHash() 方法中的代码可以自行修改,例如可以使用 SHA-256 算法来计算文件的 hash 值。使用 Set 集合来判断文件是否重复,如果集合中已经包含了该文件的 hash 值,则说明该文件已经存在,否则将该文件的 hash 值添加到集合中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值