The principle of Java HashSet face questions of?

HashSet implementation principle?
First of all, we need to know that it is a realization Set, so to ensure that none of repeated elements.
Set the one hand, the most important is to find an operator. And usually we will choose

HashSet to achieve, because it specifically optimized for fast lookup.
HashSet using a hash function, then it among the elements of disorder will be found. Which is to allow the element is null.

First a summary of the principle implemented:
(1) HashMap based implementation, a default constructor is to build an initial capacity of 16, the load factor is 0.75 HashMap. HashMap object encapsulates a collection to store all the elements of the set of all elements into a HashSet HashMap actually be held by the key, and the HashMap is stored a the PRESENT value, which is a static Object object.

(2) When we try to put a class of objects as a HashMap key, or trying to put HashSet object of this class to save, to rewrite the class equals (Object obj) method and hashCode () method is very important, and either return these two methods must be consistent: if two class hashCode () returns the value of the same, which method also should return true comparison by equals (). Generally speaking, all involved in the calculation hashCode () Returns the value of the key attribute, it should be used as the equals () standard for comparison.

Other operations (3) HashSet are based on the HashMap.

In this, as we explain the HashSet implementation principle:
it is based, the underlying HashSet HashMap implemented using HashMap to hold all the elements, so HashSet is easy to implement, HashSet related operations, related methods are basically the underlying HashMap directly call accomplished, HashSet source code is as follows:

import java.util.AbstractSet;
import java.util.Collection;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Set;

import javax.swing.text.html.HTMLDocument.Iterator;

public class HashSet<E>
extends AbstractSet<E>
implements Set<E>, Cloneable, java.io.Serializable
{
static final long serialVersionUID = -5024744406713321676L;

// use the underlying HashSet HashMap to store all elements.
transient HashMap Private <E, Object> Map;

// definition of a virtual object as HashMap Object of value, this object is defined as static final.
private static final Object PRESENT = new Object ();


// default no-argument constructor Constructs an empty HashSet.
//
// initializes the actual underlying empty HashMap, and uses the default initial capacity and load factor to 16 0.75.

HashSet public () {
Map new new = the HashMap <E, Object> ();
}


// Construct a new set containing the elements of the specified collection.
//
// actual underlying default load factor and 0.75 is sufficient to contain the specified
// Collection of all elements in the initial capacity to create a HashMap.
// @param c collection whose elements will be stored in this set.

HashSet public (Collection C <the extends E?>) {
Map new new = the HashMap <E, Object> (Math.max ((int) (c.size () / 75f) +. 1, 16).);
the addAll (C) ;
}


// specified initialCapacity and loadFactor construct an empty HashSet.
//
// underlying a corresponding actual parameter Constructs an empty HashMap.
// @param initialCapacity initial capacity.
// @param loadFactor load factor.

HashSet public (int initialCapacity, a float loadFactor) {
Map new new = the HashMap <E, Object> (initialCapacity, loadFactor);
}


// specified initialCapacity construct an empty HashSet.
//
// actual parameters and corresponding underlying load factor is 0.75 loadFactor Constructs an empty HashMap.
// @param initialCapacity initial capacity.

HashSet public (int initialCapacity) {
Map new new = the HashMap <E, Object> (initialCapacity);
}


// specified initialCapacity and loadFactor construct a new, empty linked hash set.
// This constructor to package access, is not open to the public, but the actual support for the LinkedHashSet.
//
// actual parameters at a specified underlying structure to achieve a null LinkedHashMap instance.
// @param initialCapacity initial capacity.
// @param loadFactor load factor.
// @param dummy mark.

HashSet (int initialCapacity, loadFactor a float, Boolean dummy) {
Map a LinkedHashMap new new = <E, Object> (initialCapacity, loadFactor);
}


// returns the elements in this set iterator. Order they are returned is not specific.
//
// bottom of the actual call to the underlying HashMap keySet to return all of the key.
// HashSet the visible elements, but the key is stored in the bottom of the HashMap,
// value using a static final Object of object identification.
// @return an Iterator this element in the set.

public Iterator<E> iterator() {
return map.keySet().iterator();

}

// returns the number of elements in this set (the set capacity).
//
// actual call bottom size HashMap () method returns the number of the Entry, get the number of elements in the Set.
// @return element in this set number (a set capacity).

int size public () {
return map.size ();
}


// if this set contains no elements, it returns true.
//
// bottom of the actual call HashMap isEmpty () determines whether the HashSet is empty.
// @return if this set contains no elements, it returns true.

Boolean isEmpty public () {
return map.isEmpty ();
}


// if this set contains the specified element, it returns true.
// More precisely, if and only if this set contains a satisfying (o == null e == null: ? O.equals (e))
// When the e element, returns true.
//
// underlying HashMap of the actual call to determine whether containsKey contains the specified key.
// @param o exist in this set of elements have been tested.
// @return if this set contains the specified element, it returns true.

Boolean the contains public (Object O) {
return map.containsKey (O);
}


// if this set contains the specified element is not already, add the specified element.
// More precisely, if this set contains no satisfying (E == null e2 == null:? E.equals (e2))
// elements e2, then add to this set specified element e.
// If this set already contains the element, the call does not change the set and returns false.
//
// bottom as actually the key element into the HashMap.
// Since the HashMap put () method when adding key-value pairs, when a new HashMap into a Key Entry in
// the same set of original Entry key (hashCode () returns the values are equal, returns true equals By comparison) ,
// Entry will cover the value of the original Entry of new add value, but the key will not have any change,
// so if an element is added to an existing HashSet, the newly added collection of elements will not be put into the HashMap,
// original elements would not be any change, which also meets the Set of elements of non-repetition characteristics.
// @param e to add this element in the set.
// @return if this set did not already contain the specified element, it returns true.

Boolean the Add public (E E) {
return map.put (E, the PRESENT) == null;
}

// If the specified element from this set, it is removed.
// More precisely, if this set contains a satisfying (o == null e == null: ? O.equals (e)) elements E,
// it is removed. If this set already contains the element, then return to true
// (or: if this set changed as a result of calling Returns true). (Once the call returns, this set will not contain the element).
//
// underlying HashMap actually call the remove method removes a specified Entry.
// @param o If an object in this set is required to remove the presence of.
// @return if this set contains the specified element, it returns true.

Remove Boolean public (Object O) {
return map.remove (O) == the PRESENT;
}


// Removes from this set all of the elements. After this call returns, the set will be empty.
//
// actual underlying calling clear way to clear Entry HashMap of all the elements.

void Clear public () {
map.clear ();
}

// return a shallow copy of this HashSet instance: the elements themselves are not copied.

// bottom of the actual call clone HashMap () method to get a shallow copy of the HashMap, and HashSet to set in.

Object clone public () {
the try {
HashSet <E> = the newset (HashSet <E>) to super.clone ();
newSet.map = (the HashMap <E, Object>) map.clone ();
return the newset;
} the catch ( E CloneNotSupportedException) {
the throw an InternalError new new ();
}
}
}
---------------------

Guess you like

Origin www.cnblogs.com/hyhy904/p/10930703.html