package java.util;
import java.io.InvalidObjectException;
public class HashSet
extends AbstractSet
implements Set, Cloneable, java.io.Serializable{
//åºååID
static final long serialVersionUID = -5024744406713321676L;
//HashSet åºå±ç¨ HashMap æ¥åæ¾æ°æ®
//Keyå¼ç±å¤é¨ä¼ å
¥ï¼Valueåç± HashSet å
鍿¥ç»´æ¤
private transient HashMap map;
//HashMap 䏿æé®å¼å¯¹é½å
±äº«åä¸ä¸ªå¼
//峿æåå
¥ HashMap çé®å¼å¯¹é½æ¯ä½¿ç¨è¿ä¸ªå¯¹è±¡ä½ä¸ºå¼
private static final Object PRESENT = new Object();
//æ åæé 彿°ï¼HashMap 使ç¨é»è®¤çåå§å大å°åè£
è½½å å
public HashSet() {
map = new HashMap<>();
}
//使ç¨é»è®¤çè£
è½½å åï¼å¹¶ä»¥æ¤æ¥è®¡ç® HashMap çåå§å大å°
//+1 æ¯ä¸ºäºå¼¥è¡¥ç²¾åº¦æå¤±
public HashSet(Collection extends E> c) {
map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
addAll(c);
}
//为 HashMap èªå®ä¹åå§å大å°åè£
è½½å å
public HashSet(int initialCapacity, float loadFactor) {
map = new HashMap<>(initialCapacity, loadFactor);
}
//为 HashMap èªå®ä¹åå§å大å°
public HashSet(int initialCapacity) {
map = new HashMap<>(initialCapacity);
}
//æ¤æé 彿°ä¸ºå
è®¿é®æéï¼åªç¨äºå¯¹ LinkedHashSet çæ¯æ
HashSet(int initialCapacity, float loadFactor, boolean dummy) {
map = new LinkedHashMap<>(initialCapacity, loadFactor);
}
//å°å¯¹ HashSet çè¿ä»£è½¬æ¢ä¸ºå¯¹ HashMap ç Key å¼çè¿ä»£
public Iterator 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);
}
//妿 HashSet ä¸ä¸å
å«å
ç´ eï¼åæ·»å 该å
ç´ ï¼å¹¶è¿å true
//妿 HashSet ä¸å
å«å
ç´ eï¼åä¸ä¼å½±å HashSet ï¼å¹¶è¿å false
//è¯¥æ¹æ³å°å HashSet æ·»å å
ç´ e çæä½è½¬æ¢ä¸ºå HashMap æ·»å é®å¼å¯¹
//妿 HashMap ä¸å
å« key å¼ä¸ e ç¸ççç»ç¹ï¼hashCode() æ¹æ³è¿åå¼ç¸çï¼éè¿ equals() æ¹æ³æ¯è¾ä¹è¿å trueï¼
//åæ°æ·»å çç»ç¹ç value ä¼è¦çåææ°æ®ï¼ä½ key ä¸ä¼æææ¹å
//å æ¤å¦æå HashSet æ·»å ä¸ä¸ªå·²åå¨çå
ç´ æ¶ï¼å
ç´ ä¸ä¼è¢«åå
¥ HashMap ä¸
//ä»èå®ç°äº HashSet å
ç´ ä¸éå¤çç¹å¾
public boolean add(E e) {
return map.put(e, PRESENT)==null;
}
//ç§»é¤éåä¸çå
ç´ o
//妿éåä¸å
å«å
ç´ oï¼åè¿å false
public boolean remove(Object o) {
return map.remove(o)==PRESENT;
}
//æ¸
空éåä¸çå
ç´
public void clear() {
map.clear();
}
/**
* Returns a shallow copy of this HashSet instance: the elements
* themselves are not cloned.
*
* @return a shallow copy of this set
*/
@SuppressWarnings("unchecked")
public Object clone() {
try {
HashSet newSet = (HashSet) super.clone();
newSet.map = (HashMap) map.clone();
return newSet;
} catch (CloneNotSupportedException e) {
throw new InternalError(e);
}
}
/**
* Save the state of this HashSet instance to a stream (that is,
* serialize it).
*
* @serialData The capacity of the backing HashMap 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 (E e : map.keySet())
s.writeObject(e);
}
/**
* Reconstitute the HashSet 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 capacity and verify non-negative.
int capacity = s.readInt();
if (capacity < 0) {
throw new InvalidObjectException("Illegal capacity: " +
capacity);
}
// Read load factor and verify positive and non NaN.
float loadFactor = s.readFloat();
if (loadFactor <= 0 || Float.isNaN(loadFactor)) {
throw new InvalidObjectException("Illegal load factor: " +
loadFactor);
}
// Read size and verify non-negative.
int size = s.readInt();
if (size < 0) {
throw new InvalidObjectException("Illegal size: " +
size);
}
// Set the capacity according to the size and load factor ensuring that
// the HashMap is at least 25% full but clamping to maximum capacity.
capacity = (int) Math.min(size * Math.min(1 / loadFactor, 4.0f),
HashMap.MAXIMUM_CAPACITY);
// Create backing HashMap
map = (((HashSet>)this) instanceof LinkedHashSet ?
new LinkedHashMap(capacity, loadFactor) :
new HashMap(capacity, loadFactor));
// Read in all elements in the proper order.
for (int i=0; ilate-binding
* and fail-fast {@link Spliterator} over the elements in this
* set.
*
* The {@code Spliterator} reports {@link Spliterator#SIZED} and
* {@link Spliterator#DISTINCT}. Overriding implementations should document
* the reporting of additional characteristic values.
*
* @return a {@code Spliterator} over the elements in this set
* @since 1.8
*/
//为äºå¹¶è¡éåæ°æ®æºä¸çå
ç´ è设计çè¿ä»£å¨
public Spliterator spliterator() {
return new HashMap.KeySpliterator(map, 0, -1, 0, 0);
}
}