-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBloomFilterTest.java
More file actions
66 lines (55 loc) · 1.94 KB
/
BloomFilterTest.java
File metadata and controls
66 lines (55 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// javac BloomFilterTest.java
//
// java BloomFilterTest
import java.util.BitSet;
public class BloomFilterTest {
private BitSet bits;
private int size;
private int hashCount;
// Constructor
public BloomFilterTest(int m, int k) {
this.size = m;
this.hashCount = k;
this.bits = new BitSet(m);
}
// A simple hash function using the item's hashCode and a seed
private int getHash(String item, int seed) {
// Combining item and seed to create unique hashes
int hash = (item + seed).hashCode();
return Math.abs(hash % size);
}
// Add item to filter
public void add(String item) {
for (int i = 0; i < hashCount; i++) {
bits.set(getHash(item, i));
}
}
// Check if item might exist
public boolean exists(String item) {
for (int i = 0; i < hashCount; i++) {
if (!bits.get(getHash(item, i))) {
return false; // Definitely not there
}
}
return true; // Might be there
}
public static void main(String[] args) {
// Initialize: 100 bits, 3 hash functions
BloomFilterTest bf = new BloomFilterTest(100, 3);
// 1. New Testament Seed Items
String[] additions = { "Fisher-Net", "Alabaster-Jar", "Thorns-Crown", "Seamless-Robe", "Five-Loaves" };
System.out.println("--- Seeding Bloom Filter ---");
for (String item : additions) {
bf.add(item);
System.out.println("Added: " + item);
}
// 2. Testing
System.out.println("\n--- Testing Membership ---");
String[] tests = { "Fisher-Net", "Thorns-Crown", "Golden-Calf", "Centurion-Spear", "Fisher-Boat" };
for (String test : tests) {
boolean result = bf.exists(test);
String status = result ? "PROBABLY PRESENT" : "DEFINITELY ABSENT";
System.out.printf("%-18s : %s%n", test, status);
}
}
}