HashSet

HashSet


1. Summary (jdk 1.8.0_131)

1. The underlying implementation of HashSet is completed through HashMap, the added content is used as the KeySet of HashMap, and the value is a fixed value

2. Thread unsafe

3. Allow null value

4. clone is a shallow copy

2. Class
public class HashSet extends AbstractSet implements Set, Cloneable,
		Serializable {


3. Construction method

    // Member property: global map
    private transient HashMap<E,Object> map;

    // Dummy value to associate with an Object in the backing Map
    // Define the default final Object as value
    private static final Object PRESENT = new Object();

    /**
     * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
     * default initial capacity (16) and load factor (0.75).
     */
    // empty constructor
    public HashSet() {
	map = new HashMap<E,Object>();
    }

    /**
     * Constructs a new set containing the elements in the specified
     * collection.  The <tt>HashMap</tt> is created with default load factor
     * (0.75) and an initial capacity sufficient to contain the elements in
     * the specified collection.
     *
     * @param c the collection whose elements are to be placed into this set
     * @throws NullPointerException if the specified collection is null
     */
    // Construct from an existing collection
    public HashSet(Collection<? extends E> c) {
	map = new HashMap<E,Object>(Math.max((int) (c.size()/.75f) + 1, 16));
	addAll(c);
    }

    /**
     * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
     * the specified initial capacity and the specified load factor.
     *
     * @param      initialCapacity   the initial capacity of the hash map
     * @param      loadFactor        the load factor of the hash map
     * @throws     IllegalArgumentException if the initial capacity is less
     *             than zero, or if the load factor is nonpositive
     */
    // Initialize capacity and load factor
    public HashSet(int initialCapacity, float loadFactor) {
	map = new HashMap<E,Object>(initialCapacity, loadFactor);
    }

    /**
     * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
     * the specified initial capacity and default load factor (0.75).
     *
     * @param      initialCapacity   the initial capacity of the hash table
     * @throws     IllegalArgumentException if the initial capacity is less
     *             than zero
     */
    // Initialize capacity, load factor default
    public HashSet(int initialCapacity) {
	map = new HashMap<E,Object>(initialCapacity);
    }

    /**
     * Constructs a new, empty linked hash set.  (This package private
     * constructor is only used by LinkedHashSet.) The backing
     * HashMap instance is a LinkedHashMap with the specified initial
     * capacity and the specified load factor.
     *
     * @param      initialCapacity   the initial capacity of the hash map
     * @param      loadFactor        the load factor of the hash map
     * @param      dummy             ignored (distinguishes this
     *             constructor from other int, float constructor.)
     * @throws     IllegalArgumentException if the initial capacity is less
     *             than zero, or if the load factor is nonpositive
     */
    // protected access rights, only accessible within the package, this interface is not provided to the outside world
    HashSet(int initialCapacity, float loadFactor, boolean dummy) {
	map = new LinkedHashMap<E,Object>(initialCapacity, loadFactor);
    }





3. Common methods

    // return the number of keys in the collection
    public int size() {
	return map.size();
    }

    // Whether it contains a value; that is, whether the map contains a key  
    public boolean contains(Object o) {
	return map.containsKey(o);
    }

    // add element
    // If the key already exists at this time, return the existing value value, if the addition fails, return false
    // On the contrary, return null, the addition is successful
    public boolean add(E e) {
	return map.put(e, PRESENT)==null;
    }

    // When the key is deleted, the map returns the value corresponding to the current key
    public boolean remove(Object o) {
	return map.remove(o)==PRESENT;
    }




Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326803193&siteId=291194637