Simple implementation of BloomFilter filter filtering algorithm (study notes)

Source: Internet
Author: User

BloomFilter mainly includes two types of operations: add (): add an element to contains (): judge whether an element is in it (No means no, but it may not actually exist) for the contains () function: ①, if the result returns false: Then the element must not be in the list. ②. If true is returned, it is not necessarily in the list, this is the byte ing of the error BloomFilter implementation (m (byte array size) + k (hash times) + n (data volume to be stored). Since multiple hash functions involve byte storage, A ing relationship is required, that is, to know the bytes used by an element. This is what the hash function is doing. Intuitively, it is basically impossible for an element to have a byte. In general, such a hash function does not exist. Therefore, we assume that k-bit is used to represent these bytes. Generally, k-times hash is used to determine these bytes. In fact, BloomFilter generally contains m (the size of the byte array), k (hash times), and n (the amount of data to be stored ). When an element is added to implement the add () operation, the corresponding k-bit is set to 1 for k consecutive hash operations. When the query element is in the set, it is also hash for k consecutive times. If the bit obtained for k times is not all 1, false is returned; otherwise, true is returned. Traditional algorithms are optimized by changing the time space or space, while BloomFilter uses the correct rate to change the space. Original article: http://bjyjtdj.iteye.com/blog/1455029------------------------------------------------------------------------------------------------------------------------------- A simple java example of BloomFilter; package com. me; import java. util. bitSet; // BloomFilter supports the following operations: add (), contains () public class BloomFilter {private int defaultSize = 2 <20; private int basic = defaultSize-1; private BitSet bitSet; public BloomFilter () {bitSet = new BitSet (defaultSize);} // add the element to it, public void add (String url) {if (url = null) {return;} int key1 = hashA (url); int key2 = hashB (url); int key3 = hashC (url); bitSet. set (key1); bitSet. set (key2); bitSet. set (key3) ;}// judge whether an element is in the public boolean contains (String url) {if (url = null) {return true ;} int key1 = hashA (url); int key2 = hashB (url); int key3 = hashC (url); if (bitSet. get (key1) & bitSet. get (key2) & bitSet. get (key3) {return true;} return false;} private int check (int speed) {return basic & speed;} public boolean ifNotContainsSet (String url) {if (url = null) {return true;} int key1 = hashA (url); int key2 = hashB (url); int key3 = hashC (url ); if (bitSet. get (key1) & bitSet. get (key2) & bitSet. get (key3) {return true;} bitSet. set (key1); bitSet. set (key2); bitSet. set (key3); return false;} private int hashA (String url) {int speed = 0; for (int I = 0; I <url. length (); I ++) {speed = 13 * speed + url. charAt (I);} return check (speed);} private int hashB (String url) {int speed = 0; for (int I = 0; I <url. length (); I ++) {speed = 23 * speed + url. charAt (I);} return check (speed);} private int hashC (String url) {int speed = 0; for (int I = 0; I <url. length (); I ++) {speed = 34 * speed + url. charAt (I);} return check (speed);} public static void main (String [] args) {String bd =" http://www.baidu.com "; BloomFilter bloomFilter = new BloomFilter (); // not contains String tb =" http://www.taobao.com "; System. out. println (bloomFilter. contains (tb); // if not contains then add System. out. println (bloomFilter. contains (bd); bloomFilter. add (bd); System. out. println (bloomFilter. contains (bd); // if not contains then set (ifNotSet) String gg =" http://www.google.com "; System. out. println (bloomFilter. ifNotContainsSet (gg); System. out. println (bloomFilter. contains (gg ));}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.