1. First of all, we need:
-
A suitable size of BitSet to save data
-
Several different hash functions customize different hash functions through random seed seed
-
The method of adding element to digit group (Bloom filter) implements add method
-
A method implementation that determines whether a given element exists in a bit array (a Boolean filter). contains method
2. Secondly, we need to verify the bloon filter and correct the possible error, which is implemented in the code
See note for details:
import java.util.BitSet; /** * Bloon filter: it is implemented by BitSet. It is recommended to understand the common methods of BitSet data structure first. */ public class BloomFilter { /** * Size of digit group */ // Private static final int default_size = 2 < < 24; it represents the total number of actual digits in bitset. In this case, the number is many. Many basic Bloons will not misjudge private static final int DEFAULT_SIZE = 5; //In the case of only 5 digits, bron is prone to misjudgment. /** * With this array, you can create six different hash functions */ private static final int[] SEEDS = new int[]{3, 13, 46, 71, 91, 134}; /** * Digit group. Elements in an array can only be 0 or 1 */ private BitSet bits = new BitSet(DEFAULT_SIZE); /** * An array of classes containing hash functions */ private SimpleHash[] func = new SimpleHash[SEEDS.length]; /** * Initializes an array of classes containing hash functions. The hash functions in each class are different */ public BloomFilter() { // Initializing multiple different Hash functions for (int i = 0; i < SEEDS.length; i++) { func[i] = new SimpleHash(DEFAULT_SIZE, SEEDS[i]); } } /** * Add element to digit group */ public void add(Object value) { for (SimpleHash f : func) { bits.set(f.hash(value), true); } } /** * Determine whether the specified element exists in the bit array */ public boolean contains(Object value) { boolean ret = true; for (SimpleHash f : func) { ret = ret && bits.get(f.hash(value)); } return ret; } /** * Static inner class. For hash operation! */ public static class SimpleHash { private int cap; private int seed; public SimpleHash(int cap, int seed) { this.cap = cap; this.seed = seed; } /** * Calculate hash value */ public int hash(Object value) { int h; return (value == null) ? 0 : Math.abs(seed * (cap - 1) & ((h = value.hashCode()) ^ (h >>> 16))); //Five different hash functions are generated by mixing in the value of seed. } public static void main(String[] args) { String value1 = "https://javaguide.cn / "; / / deliberately obfuscate the bloon filter to check its accuracy. String value2 = "https://GitHub. COM / snailcomb "; / / interference accuracy String value3 = "https://jaavguide.cn/"; String value4 = "https://javaguie.cn/"; String value5 = "https://javaguid.cn/"; String value6 = "https://javagide.cn/"; String value10 = "https://javavguide.cn / "; / / this value is used to detect when the Boolean filter judges the error BloomFilter filter = new BloomFilter(); System.out.println(filter.contains(value1)); filter.add(value1); filter.add(value2); filter.add(value3); filter.add(value4); filter.add(value5); filter.add(value6); System.out.println(filter.contains(value1)); System.out.println(filter.contains(value3)); System.out.println(filter.contains(value10)); //This value is used to detect when the bloom filter misjudges //Output true is the condition of misjudgment. } } }