Skip to content

Instantly share code, notes, and snippets.

@AnkitKiet
Last active June 30, 2019 13:51
Show Gist options
  • Save AnkitKiet/4d5307a778cf3a77c22199ae2c67aae5 to your computer and use it in GitHub Desktop.
Save AnkitKiet/4d5307a778cf3a77c22199ae2c67aae5 to your computer and use it in GitHub Desktop.
import java.util.BitSet;
/*
* @author: Ankit.Maurya
*/
public class BloomFilter {
BitSet bitset = null;
int numOfHashFun;
int bfSize;
int hash1, hash2;
int collisionCount;
CustomHashFunction chf;
public BloomFilter(int _bitset, int _numOfHashFun) {
chf = new CustomHashFunction();
this.bitset = new BitSet(_bitset);
bitset.clear();
this.numOfHashFun = _numOfHashFun;
bfSize = bitset.size();
collisionCount = 0;
}
public boolean isPresent(String x) {
hash1 = chf.getHash(x);
hash2 = chf.getHash2(x);
for (int i = 1; i <= numOfHashFun; i++) {
if (bitset.get(Math.abs((hash1 + i * hash2) % bfSize)))
continue;
else
return false;
}
return true;
}
public void add(String x) {
if (isPresent(x)){
collisionCount++;
System.out.println(collisionCount);
}
hash1 = chf.getHash(x);
hash2 = chf.getHash2(x);
for (int i = 1; i < numOfHashFun; i++) {
bitset.set(Math.abs((hash1 + i * hash2) % bfSize));
}
}
}
package bloomfilter;
/*
* @author: Ankit.Maurya
*/
public class CustomHashFunction {
public int getHash(String x) {
int prime = 31;
String s = "upgrad";
int _tempHashCode = 1;
int hash = x.hashCode();
for (int i = 0; i < s.length(); i++)
_tempHashCode = (prime * _tempHashCode) + s.charAt(i) + hash;
return _tempHashCode;
}
public int getHash2(String x) {
int prime = 73;
String s = "blooming";
int _tempHashCode = 1;
int hash = x.hashCode();
for (int i = 0; i < s.length(); i++)
_tempHashCode = (prime * _tempHashCode) + s.charAt(i) + hash;
return _tempHashCode;
}
}
import java.io.BufferedReader;
import java.io.FileReader;
/*
* @author: Ankit.Maurya
*/
public class Main {
public static void main(String[] args) {
String name = "src/resources/top10milliondomains.csv";
String domain = null;
try {
BufferedReader brObj = new BufferedReader(new FileReader(name));
BloomFilter bloomFilter = new BloomFilter(45227937, 30);
while ((domain = brObj.readLine()) != null) {
// System.out.println(domain.replaceAll("\"",
// "").split(",")[1]);
bloomFilter.add(domain.replaceAll("\"", "").split(",")[1]);
}
System.out.println("Done Adding");
String newUrl = "abc1236.com";
if (bloomFilter.isPresent(newUrl))
System.out.println("Already Present");
else
System.out.println("Yeahhhh, You can use this.");
} catch (Exception e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment