海量資料處理-BloomFilter
阿新 • • 發佈:2019-01-28
import java.util.BitSet;
publicclass BloomFilter
{
/* BitSet初始分配2^24個bit */privatestaticfinalint DEFAULT_SIZE =1<<25;
/* 不同雜湊函式的種子,一般應取質數 */privatestaticfinalint[] seeds =newint[] { 5, 7, 11, 13, 31, 37, 61 };
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]);
}
}
// 將字串標記到bits中publicvoid add(String value)
{
for (SimpleHash f : func)
{
bits.set(f.hash(value), true);
}
}
//判斷字串是否已經被bits標記publicboolean contains(String value)
{
if (value ==null)
{
returnfalse;
}
boolean ret =true;
for (SimpleHash f : func)
{
ret = ret && bits.get(f.hash(value));
}
return ret;
}
/* 雜湊函式類 */publicstaticclass SimpleHash
{
privateint cap;
privateint seed;
public SimpleHash(int cap, int seed)
{
this.cap = cap;
this.seed = seed;
}
//hash函式,採用簡單的加權和hashpublicint hash(String value)
{
int result =0;
int len = value.length();
for (int i =0; i < len; i++)
{
result = seed * result + value.charAt(i);
}
return (cap -1) & result;
}
}
}
publicclass BloomFilter
{
/* BitSet初始分配2^24個bit */privatestaticfinalint DEFAULT_SIZE =1<<25;
/* 不同雜湊函式的種子,一般應取質數 */privatestaticfinalint[] seeds =newint[] { 5, 7, 11, 13, 31, 37, 61 };
private BitSet bits =new BitSet(DEFAULT_SIZE);
/* 雜湊函式物件 */private SimpleHash[] func =new SimpleHash[seeds.length];
{
for (int i =0; i < seeds.length; i++)
{
func[i] =new SimpleHash(DEFAULT_SIZE, seeds[i]);
}
}
// 將字串標記到bits中publicvoid add(String value)
{
for (SimpleHash f : func)
{
bits.set(f.hash(value), true);
}
}
//判斷字串是否已經被bits標記publicboolean contains(String value)
{
if (value ==null)
{
}
boolean ret =true;
for (SimpleHash f : func)
{
ret = ret && bits.get(f.hash(value));
}
return ret;
}
/* 雜湊函式類 */publicstaticclass SimpleHash
{
privateint cap;
privateint seed;
public SimpleHash(int cap, int seed)
{
this.cap = cap;
this.seed = seed;
}
//hash函式,採用簡單的加權和hashpublicint hash(String value)
{
int len = value.length();
for (int i =0; i < len; i++)
{
result = seed * result + value.charAt(i);
}
return (cap -1) & result;
}
}
}