原文鏈接http://zhhll.icu/2021/01/21/java%E5%9F%BA%E7%A1%80/%E9%9B%86%E5%90%88/HasSet%E8%AF%A6%E8%A7%A3/
HashSet詳解
HashSet是基于HashMap實作的一個單列存盤的集合類,將所有的資料存在HashMap的key值中,而value全部使用一個Object物件存盤
繼承關系
public class HashSet<E>
extends AbstractSet<E>
implements Set<E>, Cloneable, java.io.Serializable

繼承了AbstractSet類,實作了Set介面、Cloneable介面和Serializable介面,所以HashSet是支持克隆和序列化的
原始碼分析
關鍵變數
// 使用HashMap存盤資料 map的key為HashSet的元素值
private transient HashMap<E,Object> map;
// Dummy value to associate with an Object in the backing Map
// map中所有的值都是該Object物件
private static final Object PRESENT = new Object();
構造器
// 無參構造器,直接實體化一個HashMap
public HashSet() {
map = new HashMap<>();
}
/**
* 使用的是HahMap中傳入初始容量的構造器
*/
public HashSet(Collection<? extends E> c) {
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
addAll(c);
}
/**
* 使用的是HahMap中傳入初始容量和加載因子的構造器
*/
public HashSet(int initialCapacity, float loadFactor) {
map = new HashMap<>(initialCapacity, loadFactor);
}
/**
* 使用的是HahMap中傳入初始容量的構造器
*/
public HashSet(int initialCapacity) {
map = new HashMap<>(initialCapacity);
}
/**
* 該構造器是提供給LinkedHashSet使用的,不對外暴露,實體化的是LinkedHashMap
*/
HashSet(int initialCapacity, float loadFactor, boolean dummy) {
map = new LinkedHashMap<>(initialCapacity, loadFactor);
}
方法分析
HashSet的方法實作都非常簡單,直接使用封裝的HashMap來操作資料,真正執行的是HashMap的方法
/**
* 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();
}
/**
* 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);
}
/**
* 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;
}
/**
* 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();
}
由于本身的博客百度沒有收錄,博客地址http://zhhll.icu
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/254338.html
標籤:其他
上一篇:redis哨兵模式
