Repository: TheAlgorithms/Java
Pull request #3042 (2022-05-04 04:57:17)

Create BloomFilter
Create BloomFilter dataStructure

### **Describe your change:**

- [x] Add an algorithm?
- [ ] Fix a bug or typo in an existing algorithm?
- [ ] Documentation change?

#### References

<!-- Add any reference to previous pull-request or issue -->

### **Checklist:**

<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->

- [x] I have read [CONTRIBUTING.md](https://github.com/TheAlgorithms/Java/blob/master/CONTRIBUTING.md).
- [x] This pull request is all my own work -- I have not plagiarized.
- [x] I know that pull requests will not be merged if they fail the automated tests.
- [x] This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
- [x] All new Java files are placed inside an existing directory.
- [x] All filenames are in all uppercase characters with no spaces or dashes.
- [x] All functions and variable names follow Java naming conventions.
- [x] All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
- [x] If this pull request resolves one or more open issues then the commit message contains `Fixes: #{$ISSUE_NO}`.


omarmahamid: @siriak @yanglbme 

Patch:
diff --git a/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java b/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java
new file mode 100644
index 00000000..ed77bb3c
--- /dev/null
+++ b/src/main/java/com/thealgorithms/datastructures/bloomfilter/BloomFilter.java
@@ -0,0 +1,59 @@
+package com.thealgorithms.datastructures.bloomfilter;
+
+
+public class BloomFilter<T> {
+
+    private int numberOfHashFunctions;
+    private int [] bitArray;
+    private Hash<T>[] hashFunctions;
+
+    public BloomFilter(int numberOfHashFunctions, int n) {
+        this.numberOfHashFunctions = numberOfHashFunctions;
+        hashFunctions = new Hash[numberOfHashFunctions];
+        bitArray = new int[n];
+        insertHash();
+    }
+
+    private void insertHash() {
+        for (int i = 0; i < numberOfHashFunctions; i++) {
+            hashFunctions[i] = new Hash(i);
+        }
+    }
+
+    public void insert(T key) {
+        for (Hash<T> hash : hashFunctions){
+            bitArray[hash.compute(key) % bitArray.length] = 1;
+        }
+    }
+
+    public boolean contains(T key) {
+        for (Hash<T> hash : hashFunctions){
+            if (bitArray[hash.compute(key) % bitArray.length] == 0){
+                return false;
+            }
+        }
+        return true;
+    }
+
+    private class Hash<T> {
+
+        int index;
+
+        public Hash(int index){
+            this.index = index;
+        }
+
+        public int compute(T key){
+            return index * asciiString(String.valueOf(key));
+        }
+
+        private int asciiString(String word){
+            int number = 0;
+            for (int i=0;i<word.length();i++){
+                number += word.charAt(i);
+            }
+            return number;
+        }
+    }
+
+}


Test patch:
diff --git a/src/test/java/com/thealgorithms/datastructures/BloomFilterTest.java b/src/test/java/com/thealgorithms/datastructures/BloomFilterTest.java
new file mode 100644
index 00000000..f2095814
--- /dev/null
+++ b/src/test/java/com/thealgorithms/datastructures/BloomFilterTest.java
@@ -0,0 +1,28 @@
+package com.thealgorithms.datastructures.bloomfilter;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+
+public class BloomFilterTest {
+
+    @Test
+    public void test1(){
+        BloomFilter<Integer> bloomFilter = new BloomFilter<>(3,10);
+        bloomFilter.insert(3);
+        bloomFilter.insert(17);
+
+        Assertions.assertTrue(bloomFilter.contains(3));
+        Assertions.assertTrue(bloomFilter.contains(17));
+    }
+
+    @Test
+    public void test2(){
+        BloomFilter<String> bloomFilter = new BloomFilter<>(4,20);
+        bloomFilter.insert("omar");
+        bloomFilter.insert("mahamid");
+
+        Assertions.assertTrue(bloomFilter.contains("omar"));
+        Assertions.assertTrue(bloomFilter.contains("mahamid"));
+    }
+}