System.out.printf("Time to complete %s sets: %s ms\n", rounds, stop - start);
}
public static void regularBF() {
//Create a Bloom filter that has a false positive rate of 0.1 when containing 1000 elements
BloomFilter<String> bf = new FilterBuilder(1000, 0.1).buildBloomFilter();
//Add a few elements
bf.add("Just");
bf.add("a");
bf.add("test.");
//Test if they are contained
print(bf.contains("Just")); //true
print(bf.contains("a")); //true
print(bf.contains("test.")); //true
//Test with a non-existing element
print(bf.contains("WingDangDoodel")); //false
//Add 300 elements
for (int i = 0; i < 300; i++) {
String element = "Element " + i;
bf.add(element);
}
//test for false positives
for (int i = 300; i < 1000; i++) {
String element = "Element " + i;
if (bf.contains(element)) {
print(element); //two elements: 440, 669
}
}
//Compare with the expected amount
print(bf.getFalsePositiveProbability(303) * 700); //1.74
//Estimate cardinality/population
print(bf.getEstimatedPopulation()); //303.6759147801151
//Clone the Bloom filter
bf.clone();
//Reset it
bf.clear();
//add in Bulk
List<String> bulk = Arrays.asList("one", "two", "three");
bf.addAll(bulk);
print(bf.containsAll(bulk)); //true
//Create a more customized Bloom filter
BloomFilter<Integer> bf2 = new FilterBuilder()
.expectedElements(6666) //elements
.size(10000) //bits to use
.hashFunction(HashMethod.Murmur3) //our hash
.buildBloomFilter();
print("#Hashes:" + bf2.getHashes()); //2
print(FilterBuilder.optimalK(6666, 10000)); //you can also do these calculations yourself
//Create two Bloom filters with equal parameters
BloomFilter<String> one = new FilterBuilder(100, 0.1).buildBloomFilter();
BloomFilter<String> other = new FilterBuilder(100, 0.1).buildBloomFilter();
one.add("this");
other.add("that");
one.union(other);
print(one.contains("this")); //true
print(one.contains("that")); //true