HashSet结合源码分析
一、HashSet
1 /** 2 * This class implements the <tt>Set</tt> interface, backed by a hash table 3 * (actually a <tt>HashMap</tt> instance). It makes no guarantees as to the 4 * iteration order of the set; in particular, it does not guarantee that the 5 * order will remain constant over time. This class permits the <tt>null</tt> 6 * element. 7 */
HashSet是实现的set的接口,其底层主要是由HashMap来实现的,不保证存入的顺序。详细的HashMap详解http://www.cnblogs.com/houstao/p/8271362.html
1 // Dummy value to associate with an Object in the backing Map 2 //new了一个常量用量存value值 3 private static final Object PRESENT = new Object(); 4 5 /** 6 * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has 7 * default initial capacity (16) and load factor (0.75). 8 *默认的构造器底层是new了一个HashMap,默认的数组的容量是16,加载因子为0.75 9 * 10 */ 11 public HashSet() { 12 map = new HashMap<>(); 13 }
1 /** 2 * Returns the number of elements in this set (its cardinality). 3 *Hashset的长度就是map的长度 4 * @return the number of elements in this set (its cardinality) 5 */ 6 public int size() { 7 return map.size(); 8 }
/** * Returns <tt>true</tt> if this set contains no elements. *判断是否为空,底层也是调用的map的isEmpty()方法 * @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 *调用的map底层的containsKey()的方法,map中的调用的getEntry,获 *取key-value键值对的方法,如果返回值为空话,则不包含该元素,否则包 *含 */ public boolean contains(Object o) { return map.containsKey(o); } /** * 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 *add方法是调用hashMap中的put方法,从这里你可以发现,set中add的 *元素相当于map中的key,value是由Object的对象常量存的。如果添加的元**素在map中存在,通过hash(key)计算在数组中存放位置,计算该元素的*hash是都与存在的元素相同,再通过equals判断,并且返回旧的value,故在*set中是不等于null的,因此添加不上,所以说,set中的元素是不重复的,也**相当于map中的key。如果在map中不存在该元素,则返回null,故set添***加。 * * */ public boolean add(E e) { return map.put(e, PRESENT)==null; } /** * 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; } /** * Removes all of the elements from this set. * The set will be empty after this call returns. */ public void clear() { map.clear(); }
二、总结:
1、Hashset的中的元素就是HashMap中的key,因此,元素是唯一的不重复的,同时也是无序的。
2、Hashset的最底层实现是通过HashMap来实现的。
如有错误请多多指点。