布隆过滤器(Bloom Filter)的原理和实现

场景:

  • 字处理软件中,需要检查一个英语单词是否拼写正确
  • 在 FBI,一个嫌疑人的名字是否已经在嫌疑名单上
  • 在网络爬虫里,一个网址是否被访问过
  • yahoo, gmail等邮箱垃圾邮件过滤功能

涉及到爬虫,肯定会涉及到去重问题,:海量数据查找元素是否存在。

布隆过滤原理:https://www.cnblogs.com/cpselvis/p/6265825.html



import java.util.BitSet;

/*
 简单的Bloom过滤器实现
 */
public class BloomDemo {
    private static final int SIZE = 1 << 24;
    BitSet bitSet = new BitSet(SIZE);
    Hash[] hashs = new Hash[8];
    private static final int seeds[] = new int[] { 3, 5, 7, 9, 11, 13, 17, 19 };

    public static void main(String[] args) {
        System.out.println(SIZE);
        String email = "https://blog.csdn.net/qq_40374604/article/details/88974732";
        BloomDemo bloomDemo = new BloomDemo();
        System.out.println(email + "是否在列表中: " + bloomDemo.contains(email));
        bloomDemo.add(email);
        System.out.println(email + "是否在列表中: " + bloomDemo.contains(email));
        email = "https://www.imsilkroad.com/news/category/siluyiliao";
        System.out.println(email + "是否在列表中: " + bloomDemo.contains(email));
    }

    public BloomDemo() {
        for (int i = 0; i < seeds.length; i++) {
            hashs[i] = new Hash(seeds[i]);
        }
    }

    public void add(String string) {
        for (Hash hash : hashs) {
            System.out.println(hash.getHash(string));
            bitSet.set(hash.getHash(string), true);
        }
    }

    public boolean contains(String string) {
        boolean have = true;
        for (Hash hash : hashs) {
            have &= bitSet.get(hash.getHash(string));
        }
        return have;
    }

    class Hash {
        private int seed = 0;

        public Hash(int seed) {
            this.seed = seed;
        }

        public int getHash(String string) {
            int val = 0;
            int len = string.length();
            for (int i = 0; i < len; i++) {
                val = val * seed + string.charAt(i);
            }
            return val & (SIZE - 1);
        }
    }
}

有什么想法留言区留言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JavaPub-rodert

谢谢老板

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值