六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 116|回复: 0

布隆算法 BloomFilter

[复制链接]

升级  34%

3

主题

3

主题

3

主题

童生

Rank: 1

积分
17
 楼主| 发表于 2013-2-7 19:37:43 | 显示全部楼层 |阅读模式
import java.util.BitSet;/** * <p> * 布隆算法 BloomFilter * <i>http://www.googlechinablog.com/2007/07/bloom-filter.html</i> * </p> * <strong>扩展<tt>hash</tt>算法</strong> * <p> * 基本思想:在内存里面开辟一个区域,所有位置为零,然后 用不同的hash算法对要存入的数据计算hash值,每个hash * 值对内存位数求模,得到的数在内存位置上值1,置位之前需要现 判断是否已经置位,对于插入的值,所有的位置都是1时, 才认为 是重复 * </p> *  * @author Rex.wong */public class BloomFilter {private static int defaultSize = 2 << 24;// 2的24次方。private static int basic = defaultSize - 1;private static BitSet bits;// 内存置位static int check(int h) {return basic & h;}static int hash(int h) {h ^= (h >>> 20) ^ (h >>> 12);return h ^ (h >>> 7) ^ (h >>> 4);}public BloomFilter() {bits = new BitSet(defaultSize);}public boolean contains(String url) {if (url == null) {return true;}int pos1 = check(hash(url.hashCode() + 2));int pos2 = check(hash(url.hashCode() + 4));int pos3 = check(hash(url.hashCode() + 6));if (bits.get(pos1) && bits.get(pos2) && bits.get(pos3)) {return true;}return false;}public void add(String url) {if (url == null) {return;}int pos1 = check(hash(url.hashCode() + 2));int pos2 = check(hash(url.hashCode() + 4));int pos3 = check(hash(url.hashCode() + 6));bits.set(pos1);bits.set(pos2);bits.set(pos3);}public static void main(String[] args) {String ss = "ssss";BloomFilter bf = new BloomFilter();bf.add(ss);System.out.println(bf.contains(ss));}} 
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表