Java.util.HashSet Source Analysis _______1.1.2

Source: Internet
Author: User
1.java Set Frame chart


2. Owning Package

Package java.util;

3. Inheritance and implementation relationships

public class hashset<e>
    extends abstractset<e>
    implements Set<e>, Cloneable, Java.io.Serializable

4. Preparatory work

A class hashset is actually an instance of a hashmap that does not guarantee the iteration order of the collection, nor does it guarantee that the order is immutable, allowing the use of NULL elements. Suggest first look HashMap source analysis: Java set of HashMap source code analysis
5. Property

Supported by a hash table (which is actually a HashMap instance).
    private transient hashmap<e,object> map;

    /* Because HashSet is a single value operation, and HashMap is the form of key-value pairs, because the HashSet element value as the HashMap key, then the corresponding value is present.
	 * SATISFY HASHMAP structure * * 
    private static final Object PRESENT = new Object ();

6. Construction Method

/** Construction Method 1:
     * Construct an initialization capacity of 16, load factor of 0.75 HashMap/public
    HashSet () {
        map = new hashmap<> ();
    }
/** Method 2:
     * public
    hashset (collection< extends e> c) {
        map = new, using the HashMap set parameter as the parameter of the construction method * Hashmap<> (Math.max (int) (C.size ()/.75f) + 1));
        AddAll (c);
    }
	public boolean addall (collection<? extends e> c) {
        Boolean modified = false;
        For (e e:c)
            if (Add (e))
                modified = true;
        return modified;
    }
/** Construction Method 3:
     * Enter an initialization capacity and load factor construction method HashMap
     */public
    hashset (int initialcapacity, float loadfactor) {
        map = new Hashmap<> (initialcapacity, loadfactor);
    }
/** Construction Method 4:
     * Construct method for input initialization capacity HASHMAP/
     public
    hashset (int initialcapacity) {
        map = new Hashmap<> ( initialcapacity);
    }

7. Methods

Add Method:

/**
     * Using the HashMap put method, if the added element already exists in HashMap, return True, otherwise return false
     *
    /public boolean Add (e) {
        returns Map.put (E, PRESENT) ==null;
    

Remove method:

/**
     * Using the Remove method in the HashMap, the value of the corresponding entry element is returned in the HashMap, and the value is compared with the present value stored.
	 * If the value is the same the deletion succeeds, otherwise the failure
    /public boolean remove (Object o) {return
        map.remove (o) ==present;
    }



-----------------------------The source is jdk1.7 version of the




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.