guava布隆過濾器的使用


  http://blog.csdn.net/jiaomeng/article/details/1495500中這么介紹布隆過濾器

  Bloom Filter是一種空間效率很高的隨機數據結構,它利用位數組很簡潔地表示一個集合,並能判斷一個元素是否屬於這個集合。Bloom Filter的這種高效是有一定代價的:在判斷一個元素是否屬於某個集合時,有可能會把不屬於這個集合的元素誤認為屬於這個集合(false positive)。因此,Bloom Filter不適合那些“零錯誤”的應用場合。而在能容忍低錯誤率的應用場合下,Bloom Filter通過極少的錯誤換取了存儲空間的極大節省。

  http://www.360doc.com/content/14/0914/13/7703112_409376352.shtml中討論了使用布隆過濾器實現java緩存的架構。開源爬蟲webmagic,也實現了一個基於它的url隊列的構件。u

  guava中這樣介紹它,A Bloom filter offers an approximate containment test with one-sided error: if it claims that an element is contained in it, this might be in error, but if it claims that an element is not contained in it, then this is definitely true.下面的這個例子使用的是guava-18.0.jar。

  

package com;

import java.nio.charset.Charset;

import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels;

public class Bloom {
	
	public static void main(String[] args) {
		BloomFilter<String> b = BloomFilter.create(Funnels.stringFunnel(Charset.forName("utf-8")), 1000, 0.000001);
		b.put("121");
		b.put("122");
		b.put("123");
		System.out.println(b.mightContain("12321"));
		BloomFilter<String> b1 = BloomFilter.create(Funnels.stringFunnel(Charset.forName("utf-8")), 1000, 0.000001);
		b1.put("aba");
		b1.put("abb");
		b1.put("abc");
		b1.putAll(b);
		System.out.println(b1.mightContain("123"));
	}

}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM