複習下java的常用集合框架,java常用的集合大致有兩種類型,一種實現了Collection接口表示普通的集合,一種實現了Map接口表示嚴格的鍵值對,其中Collection又分爲List接口類型和Set接口類型,其中List接口類型允許集合中元素重複,例如ArrayList,Vector,LinkedList,Stack,Set接口實現的集合不允許重複元素,例如HashSet和TreeSet。
HashSet從類名就可以看出,它使用HashMap的存儲方法存儲元素,其中也就使用了HashMap的key那一部分,value默認爲Object,所以它的元素不會重複。
package java.util;
public class HashSet<E>
extends AbstractSet<E>
implements Set<E>, Cloneable, java.io.Serializable
{
static final long serialVersionUID = -5024744406713321676L;
//存儲元素的內部集合
private transient HashMap<E,Object> map;
//因爲它只需要保存不重複的key元素,value元素默認爲一個Object就好
private static final Object PRESENT = new Object();
/**
* 無參構造函數,默認的容量16,加載因子爲3/4
*/
public HashSet() {
map = new HashMap<E,Object>();
}
/**
* 略,Math.max是求最大值,找到一個容量的參數,要麼是c集合已有個數除以0.75,要麼是16
*/
public HashSet(Collection<? extends E> c) {
map = new HashMap<E,Object>(Math.max((int) (c.size()/.75f) + 1, 16));
addAll(c);
}
/**
* 略
*/
public HashSet(int initialCapacity, float loadFactor) {
map = new HashMap<E,Object>(initialCapacity, loadFactor);
}
/**
* 略
*/
public HashSet(int initialCapacity) {
map = new HashMap<E,Object>(initialCapacity);
}
/**
* 參考了他人的說法:
以指定的initialCapacity和loadFactor構造一個新的空鏈接哈希集合。
* 此構造函數爲包訪問權限,不對外公開,實際只是是對LinkedHashSet的支持。
* 實際底層會以指定的參數構造一個空LinkedHashMap實例來實現。
*/
HashSet(int initialCapacity, float loadFactor, boolean dummy) {
map = new LinkedHashMap<E,Object>(initialCapacity, loadFactor);
}
/**
* 返回迭代器,使用的鍵Set視圖集合返回的迭代器
*/
public Iterator<E> iterator() {
return map.keySet().iterator();
}
/**
*返回有多少個元素
*/
public int size() {
return map.size();
}
/**
* 判斷是否爲空
*/
public boolean isEmpty() {
return map.isEmpty();
}
/**
* 包含
*/
public boolean contains(Object o) {
return map.containsKey(o);
}
/**
* 增加一個元素,增加在key的位置上
*/
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
/**
* 刪除
*/
public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}
/**
* 刪除所有
*/
public void clear() {
map.clear();
}
/**
* 克隆,調用的底層克隆方法
*/
public Object clone() {
try {
HashSet<E> newSet = (HashSet<E>) super.clone();
newSet.map = (HashMap<E, Object>) map.clone();
return newSet;
} catch (CloneNotSupportedException e) {
throw new InternalError();
}
}
/**
* Save the state of this <tt>HashSet</tt> instance to a stream (that is,
* serialize it).
*
* @serialData The capacity of the backing <tt>HashMap</tt> instance
* (int), and its load factor (float) are emitted, followed by
* the size of the set (the number of elements it contains)
* (int), followed by all of its elements (each an Object) in
* no particular order.
*/
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
// Write out any hidden serialization magic
s.defaultWriteObject();
// Write out HashMap capacity and load factor
s.writeInt(map.capacity());
s.writeFloat(map.loadFactor());
// Write out size
s.writeInt(map.size());
// Write out all elements in the proper order.
for (Iterator i=map.keySet().iterator(); i.hasNext(); )
s.writeObject(i.next());
}
/**
* Reconstitute the <tt>HashSet</tt> instance from a stream (that is,
* deserialize it).
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in any hidden serialization magic
s.defaultReadObject();
// Read in HashMap capacity and load factor and create backing HashMap
int capacity = s.readInt();
float loadFactor = s.readFloat();
map = (((HashSet)this) instanceof LinkedHashSet ?
new LinkedHashMap<E,Object>(capacity, loadFactor) :
new HashMap<E,Object>(capacity, loadFactor));
// Read in size
int size = s.readInt();
// Read in all elements in the proper order.
for (int i=0; i<size; i++) {
E e = (E) s.readObject();
map.put(e, PRESENT);
}
}
}
可以看出HashSet非常簡單,非線程安全,向HashMap一樣沒有容量限制