Java implementation to the heavy cloth-lung filter (bloomfilter)

Source: Internet
Author: User

Recently used to find the duplicate elements in the list, the results to share with you, you also as a note.
The implementation of the Bloomfilter filter found on the Web:

public class Bloomfilter {private static final int default_size = 2 << 24;
    private static final int[] seeds = new int[] {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_si
        ZE, Seeds[i]); } public void Add (String value) {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;
        }//Internal class, Simplehash 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 = 0;
            int len = Value.length ();
            for (int i = 0; i < len; i++) {result = Seed * result + value.charat (i);
        Return (cap-1) & result; }
    }

}

How to use:

public static void Main (string[] args) {
        bloomfilter bf = new Bloomfilter ();
        List<string> STRs = new arraylist<string> ();
        Strs.add ("123456");
        Strs.add ("Hello word");
        Strs.add ("Transdocid");
        Strs.add ("123456");
        Strs.add ("Transdocid");
        Strs.add ("Hello word");
        Strs.add ("test");
        for (int i=0;i<strs.size (); i++) {
            String s = strs.get (i);
            Boolean bl = Bf.contains (s);
            if (BL) {
                System.out.println (i+ "," +s);
            } else{
                Bf.add (s);
            }
        }
    

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.