bitSet,布隆过滤器等

bitSet

java bitSet类是一种特殊类型的数组,用于存储位值,提供的方法本质上是位运算,所以对于一些数据的处理特别快。

应用场景

示例

// 数据准备
int i = 0;
List<Apple> apples = new ArrayList<>();
while (i < 10000000) {
    Apple apple = new Apple();
    if (i % 4 == 0) {
        apple.setCountry("china");
    }
    if (i % 6 == 0) {
        apple.setWight(10);
    }
    apple.setSeqNo(i);
    apples.add(apple);
    i++;
}

BitSet chinaBitSet = new BitSet();
for (Apple apple : apples) {
    if ("china".equals(apple.getCountry())) {
        chinaBitSet.set(apple.getSeqNo());
    }
}

BitSet wightSet = new BitSet();
for (Apple apple : apples) {
    if (Integer.valueOf(10).equals(apple.getWight())) {
        wightSet.set(apple.getSeqNo());
    }
}

// bitset求交集
long startTimeOfBitSet = System.currentTimeMillis();
wightSet.and(chinaBitSet);
long endTimeOfBitSet = System.currentTimeMillis();
System.out.println("bitset method cost time---" + (endTimeOfBitSet - startTimeOfBitSet));

// 正常求交集
long startTimeOfNormal = System.currentTimeMillis();
apples = apples.stream().filter(a -> "china".equals(a.getCountry()) && Integer.valueOf(10).equals(a.getWight())).collect(Collectors.toList());
long endTimeOfNormal = System.currentTimeMillis();
System.out.println("normal method cost time---" + (endTimeOfNormal - startTimeOfNormal));

布隆过滤器

未完待续….