Guava 提供了一组散列(哈希)相关的工具类和方法,包括哈希函数接口、哈希算法实现、哈希码(HashCode)类、布隆过滤器(BloomFilter)等等。
Guava 提供了多种哈希函数实现,如:
HashFunction hashFunction = Hashing.sha256();
HashCode hashCode = hashFunction.newHasher()
.putString("example", Charsets.UTF_8)
.putInt(123)
.hash();
String hexHashCode = hashCode.toString();
System.out.println("Hash code: " + hexHashCode);
HashCode hashCode1 = Hashing.sha256().hashString("example1", Charsets.UTF_8);
HashCode hashCode2 = Hashing.sha256().hashString("example2", Charsets.UTF_8);
boolean isEqual = hashCode1.equals(hashCode2);
System.out.println("Hash codes are equal: " + isEqual);
int expectedInsertions = 1000;
double falsePositiveProbability = 0.01;
BloomFilter<String> bloomFilter = BloomFilter.create(
Funnels.stringFunnel(Charsets.UTF_8),
expectedInsertions,
falsePositiveProbability
);
bloomFilter.put("example1");
bloomFilter.put("example2");
boolean mightContain = bloomFilter.mightContain("example1");
System.out.println("Might contain example1: " + mightContain);
更多【guava-Google Guava 散列工具使用详解】相关视频教程:www.yxfzedu.com