There are a lot of explanations on the Internet, there is no longer a description of Bloom filter, directly on the code (note: The code refers to the implementation of other blogs on the internet, such as Prum filter (Bloom filter) Java implementation)
/** * Project Name: Spidercrawler * File name: Bloomfiltertest.java * Author: zhouyh * Time: 2014-8-29 02:54:56 * Description: TODO (describe what the file does with a word)
* * Package com.utiltest;
Import Java.io.BufferedReader;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.InputStreamReader;
Import Java.util.BitSet;
/** * Class Name: Bloomfiltertest * Package Name: Com.utiltest * Author: zhouyh * Time: 2014-8-29 02:54:56 * Description: Prum filter, traditional cloth-long filter does not support removing members from the collection
* * public class Bloomfiltertest {//default_size is 2 29 times, that is, the left 28-bit private static final int default_size = 2<<28; * * * different hash function seed, general prime number * Seeds array has 8 values, the representative uses 8 different hash functions */private int[] seeds = new int[]{3, 5, 7, 11, 13, 31, 37, 61}
;
* * Initializes a set of bits of a given size * Bitset is actually a vector composed of "bits".
* If you want to save a lot of "open-close" information efficiently, you should use Bitset.
* Private Bitset Bitsets = new Bitset (default_size);
Constructing a hash function object Private simplehash[] Hashfuns = new Simplehash[seeds.length];
Prum Filter configuration File Store path private String path = ""; Public BlOomfiltertest (String path) {/** * gives all hash values, totaling seeds.length hash values.
Total 8 bits.
* by calling Simplehash.hash (), the hash value can be computed based on 8 hash functions.
* Incoming default_size (the length of the final string), Seeds[i] (a specified prime number) to get the location of the desired hash value.
* for (int i=0; i<seeds.length; i++) {hashfuns[i] = new Simplehash (default_size, seeds[i]);
}//config file path address This.path = path; /** * * Method Name: Add * Author: zhouyh * Date Created: 2014-8-30 02:07:35 * Description: Mark the given string to Bitsets, that is, the position of the 8 function value of the string is 1 * @param Value */public synchronized void Add (String value) {for (Simplehash hashfun:hashfuns) {Bitsets.set (Hashfun.hash
(value), true); }/** * * Method Name: Isexit * Author: zhouyh * Date Created: 2014-8-30 02:12:30 * Description: Determines whether a given string already exists in Bloofilter, if present returns True, does not exist Returns false * @param value * @return/Public synchronized Boolean isexit (String value) {//To determine if the passed-in value is null if (NULL =
= value) {return false; for (Simplehash hashfun:hashfuns) {if!bitsets.get (Hashfun.hash (value)) {//If you judge that one of the 8 hash function values does not exist, you can judge that there is no B Loreturn False in Ofilter;
} return true; /** * * Method Name: Init * Author: zhouyh * Date Created: 2014-8-30 02:28:49 * Description: Read config file/public void init () {File file
= new File (path);
FileInputStream in = null;
try {in = new FileInputStream (file);
Long lt = System.currenttimemillis ();
Read (in);
System.out.println (System.currenttimemillis ()-lt);
System.out.println (Runtime.getruntime (). TotalMemory ());
}catch (Exception e) {e.printstacktrace ();
}finally{try {if (in!=null) {in.close ();
in = null;
The catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
/** * * Method Name: Read * Author: zhouyh * Date: 2014-8-30 02:26:59 * Description: Initializes the Bloomfilter * @param in according to the incoming stream
/private void Read (InputStream in) {if (null = =) {///If in is null, returns return;
int i = 0;
InputStreamReader reader = null;
try {//Create input stream reader = new InputStreamReader (in, "UTF-8"); BufferEdreader Buffreader = new BufferedReader (reader, 512);
String Theword = null;
do {i++;
Theword = Buffreader.readline ();
If Theword is not null and empty, add bloomfilter if (theword!=null &&!theword.trim (). Equals ("")) {Add (Theword);
} if (i%10000 = = 0) {System.out.println (i);
} while (Theword!= null);
catch (IOException e) {e.printstacktrace ();
finally{//Close stream try {if (reader!= null) {reader.close ();
reader = null;
} if (in!= null) {in.close ();
in = null;
} catch (IOException e) {//Todo:handle exception e.printstacktrace ();
/** * Method Name: Main * Author: zhouyh * Date Created: 2014-8-29 02:54:56 * Description: TODO (Describe the effect of this method in one sentence) * @param args */public static void main (string[] args) {//TODO auto-generated method stub bloomfiltertest bloomfiltertest = NE
W bloomfiltertest ("F:/fetchedurls.txt");
Bloomfiltertest.init (); System.out.println (Bloomfiltertest.isexit ("http://www.plating.org/news_info.asp?pid=28&id=2857"));
The public static class Simplehash {* * * cap is default_size, that is, the value of the maximum string for the result * seed is a key value for calculating the hash value, corresponding to the seeds array above
* * private int cap;
private int seed; /** * Constructor * Author: zhouyh * @param cap * @param seed/public Simplehash (int caps, int seed) {this.
Cap = cap;
This.seed = seed; /** * * Method Name: Hash * Author: zhouyh * Date Created: 2014-8-30 01:47:10 * Description: Compute hash function, user can choose other better hash function * @param
Value * @return */public int hash (String value) {int = 0;
int length = Value.length ();
for (int i=0; i<length; i++) {result = Seed*result + Value.charat (i);
Return (cap-1) & result;
}
}
}In actual use, the effect is good, mainly used in the crawler (crawler) on the site of the sentence on the weight