Skip to content

Instantly share code, notes, and snippets.

@rakeshopensource
Created September 14, 2023 16:02
Show Gist options
  • Save rakeshopensource/7d0436a5e3f160b2714646d8c1fae9e3 to your computer and use it in GitHub Desktop.
Save rakeshopensource/7d0436a5e3f160b2714646d8c1fae9e3 to your computer and use it in GitHub Desktop.
Simple, basic Bloom filter simulation in Java that uses multiple hash functions to efficiently test if an element might be in a set.
package org.rakeshopensource.systemdesign;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
import java.util.function.Function;
public class BloomFilter <T> {
private final int size;
private final BitSet bitSet;
private final List<Function<T, Integer>> hashFunctions;
public BloomFilter(int size, List<Function<T, Integer>> hashFunctions) {
this.size = size;
this.bitSet = new BitSet(this.size);
this.hashFunctions = hashFunctions;
}
public void add(T element){
for(Function<T,Integer> hashFunction : hashFunctions){
int hash = Math.abs(hashFunction.apply(element)) % size;
bitSet.set(hash);
}
}
public boolean mightContain(T element){
for(Function<T,Integer> hashFunction : hashFunctions){
int hash = Math.abs(hashFunction.apply(element)) % size;
if(!bitSet.get(hash)){
return false;
}
}
return true;
}
public static void main(String[] args) {
List<Function<String,Integer>> hashFunctions = new ArrayList<Function<String,Integer>>();
//Function<String, Integer> fun2 = str -> str.hashCode();
Function<String, Integer> fun1 = String::hashCode;
Function<String, Integer> fun2 = String::length;
Function<String, Integer> fun3 = (str) -> str.hashCode() * 31;
hashFunctions.add(fun1);
hashFunctions.add(fun2);
hashFunctions.add(fun3);
BloomFilter<String> bloomFilter = new BloomFilter<String>(640, hashFunctions);
bloomFilter.add("apple");
System.out.println(bloomFilter.mightContain("apple"));
System.out.println(bloomFilter.mightContain("banana"));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment