HashSet (jdk 1.7)的繼承關係以下:java
HashSet是使用HashMap實現的一個沒有重複元素的集合。HashSet用法以下:ui
HashSet<String> hashSet = new HashSet<String>(); hashSet.add("java001"); hashSet.add("java01"); hashSet.add("java011"); hashSet.add("java002"); hashSet.add("java004");
從HashSet的add()方法能夠看出,只有一個參數,並無【鍵-值對】。this
實際上是HashSet只使用了HashMap的key,value統一是一個固定的Object,所以保證沒有重複元素的方法,也是使用的HashMap的key來保證的。orm
既然HashSet是使用HashMap來實現的,那麼HashSet必然會有一個HashMap的對象,即:對象
private transient HashMap<E,Object> map; // Dummy value to associate with an Object in the backing Map 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). */ public HashSet() { map = new HashMap<>(); }
其中,PRESENT = new Object()是用來做爲HashMap的value使用的。能夠經過其add()方法看到blog
/** * Adds the specified element to this set if it is not already present. * More formally, adds the specified element <tt>e</tt> to this set if * this set contains no element <tt>e2</tt> such that * <tt>(e==null ? e2==null : e.equals(e2))</tt>. * If this set already contains the element, the call leaves the set * unchanged and returns <tt>false</tt>. * * @param e element to be added to this set * @return <tt>true</tt> if this set did not already contain the specified * element */ public boolean add(E e) { return map.put(e, PRESENT)==null; }
在add()方法,添加的元素做爲key,PRESENT做爲value。繼承
remove()方法也是調用的HashMap的remove()方法ci
/** * Removes the specified element from this set if it is present. * More formally, removes an element <tt>e</tt> such that * <tt>(o==null ? e==null : o.equals(e))</tt>, * if this set contains such an element. Returns <tt>true</tt> if * this set contained the element (or equivalently, if this set * changed as a result of the call). (This set will not contain the * element once the call returns.) * * @param o object to be removed from this set, if present * @return <tt>true</tt> if the set contained the specified element */ public boolean remove(Object o) { return map.remove(o)==PRESENT; }
一樣,size() / isEmpty() / contains() / clear() 都是調用的HashMap相應的方法。element
/** * Returns the number of elements in this set (its cardinality). * * @return the number of elements in this set (its cardinality) */ public int size() { return map.size(); } /** * Returns <tt>true</tt> if this set contains no elements. * * @return <tt>true</tt> if this set contains no elements */ public boolean isEmpty() { return map.isEmpty(); } /** * Returns <tt>true</tt> if this set contains the specified element. * More formally, returns <tt>true</tt> if and only if this set * contains an element <tt>e</tt> such that * <tt>(o==null ? e==null : o.equals(e))</tt>. * * @param o element whose presence in this set is to be tested * @return <tt>true</tt> if this set contains the specified element */ public boolean contains(Object o) { return map.containsKey(o); } /** * Removes all of the elements from this set. * The set will be empty after this call returns. */ public void clear() { map.clear(); }
另外,HashSet中有個更明顯的方法來講明【HashSet只使用了HashMap的key】,即iterator()方法rem
/** * Returns an iterator over the elements in this set. The elements * are returned in no particular order. * * @return an Iterator over the elements in this set * @see ConcurrentModificationException */ public Iterator<E> iterator() { return map.keySet().iterator(); }