java实现简易版布隆过滤器

public class BloomFilters {

    /**
     * 数组长度
     */
    private int arraySize;

    /**
     * 数组
     */
    private int[] array;

    public BloomFilters(int arraySize){
        this.arraySize = arraySize;
        this.array = new int[arraySize];
    }

    public void add(String key){
        int hash1 = hashcode_1(key);
        int hash2 = hashcode_2(key);
        int hash3 = hashcode_3(key);
        array[hash1 % arraySize] = 1;
        array[hash2 % arraySize] = 1;
        array[hash3 % arraySize] = 1;
    }

    public boolean mightContain(String code){
        int hash1 = hashcode_1(code);
        int hash2 = hashcode_2(code);
        int hash3 = hashcode_3(code);
        return array[hash1 % arraySize] == 1 && array[hash2 % arraySize] == 1 && array[hash3 % arraySize] == 1;
    }

    /**
     * hash函数1
     * @param key
     * @return
     */
    private int hashcode_1(String key){
        int hash = 0;
        for(int i = 0; i < key.length(); i++){
            hash += 33 + key.charAt(i);
        }
        return Math.abs(hash);
    }

    /**
     * hash 算法2
     * @param data
     * @return
     */
    private int hashcode_2(String data) {
        final int p = 16777619;
        int hash = (int) 2166136261L;
        for (int i = 0; i < data.length(); i++) {
            hash = (hash ^ data.charAt(i)) * p;
        }
        hash += hash << 13;
        hash ^= hash >> 7;
        hash += hash << 3;
        hash ^= hash >> 17;
        hash += hash << 5;
        return Math.abs(hash);
    }

    /**
     *  hash 算法3
     * @param key
     * @return
     */
    private int hashcode_3(String key) {
        int hash, i;
        for (hash = 0, i = 0; i < key.length(); ++i) {
            hash += key.charAt(i);
            hash += (hash << 10);
            hash ^= (hash >> 6);
        }
        hash += (hash << 3);
        hash ^= (hash >> 11);
        hash += (hash << 15);
        return Math.abs(hash);
    }

    public static void main(String[] args) {
        BloomFilters bloomFilters = new BloomFilters(10000000);
        for (int i = 0; i < 1000000; i++) {
            if(i == 12334 || i == 23223 || i == 2332){
                continue;
            }
            bloomFilters.add(i + "");
        }
        System.out.println(bloomFilters.mightContain("12334"));
        System.out.println(bloomFilters.mightContain("23223"));
        System.out.println(bloomFilters.mightContain("2332"));
        System.out.println(bloomFilters.mightContain("23324"));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
布隆过滤器是一种空间效率非常高的随机数据结构,用于快速检索一个元素是否在一个集合中。它有可能会误判,但不会漏判。 Java实现布隆过滤器需要用到以下几个类: 1. BitSet类:Java提供的一个位集合类,用于表示一个由0和1组成的序列。 2. Hash函数:布隆过滤器需要用到多个不同的Hash函数,用于将元素映射到不同的位上。在Java中可以使用MessageDigest类中的MD5、SHA等Hash函数。 下面是一个简单的Java实现布隆过滤器代码: ``` import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.BitSet; public class BloomFilter { private static final int DEFAULT_SIZE = 2 << 24; // 布隆过滤器的默认大小 private static final int[] seeds = new int[]{7, 11, 13, 31, 37, 61}; // 随机种子,用于生成不同的Hash函数 private BitSet bits = new BitSet(DEFAULT_SIZE); private SimpleHash[] func = new SimpleHash[seeds.length]; public BloomFilter() { for (int i = 0; i < seeds.length; i++) { func[i] = new SimpleHash(DEFAULT_SIZE, seeds[i]); // 初始化Hash函数 } } // 将元素添加到布隆过滤器中 public void add(String value) { if (value != null) { for (SimpleHash f : func) { bits.set(f.hash(value), true); } } } // 判断布隆过滤器是否包含指定元素 public boolean contains(String value) { if (value == null) { return false; } boolean ret = true; for (SimpleHash f : func) { ret = ret && bits.get(f.hash(value)); } return ret; } // Hash函数 public static class SimpleHash { private int cap; private int seed; public SimpleHash(int cap, int seed) { this.cap = cap; this.seed = seed; } public int hash(String value) { int result = 0; try { byte[] bytes = MessageDigest.getInstance("MD5").digest(value.getBytes()); for (byte b : bytes) { result += b; } } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } return (cap - 1) & (result * seed); // 生成Hash值 } } public static void main(String[] args) { BloomFilter filter = new BloomFilter(); filter.add("hello"); filter.add("world"); System.out.println(filter.contains("hello")); // true System.out.println(filter.contains("world")); // true System.out.println(filter.contains("java")); // false } } ``` 上面的代码中,我们使用了6个不同的随机种子,生成了6个不同的Hash函数。对于一个元素,我们使用每个Hash函数将其映射到6个不同的位上,然后将这6个位都设为1。当我们需要判断一个元素是否在布隆过滤器中时,我们使用每个Hash函数将其映射到6个不同的位上,然后判断这6个位是否都为1,如果都为1,则说明元素可能存在于布隆过滤器中,否则一定不存在于布隆过滤器中。 需要注意的是,布隆过滤器有可能会误判,即判断一个不存在的元素在布隆过滤器中存在。因此,在使用布隆过滤器时,需要根据实际情况来选择合适的参数,以控制误判率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值