java中HashSet的实现

public interface Set extends Collection {

    int size();//大小

    boolean isEmpty();//是否为空

    boolean contains(Object o);//是包含指定的o

    Iterator iterator();//迭代器

    Object[] toArray();//将set中的元素转换成数组

     T[] toArray(T[] a);//将set中的元素转换成指定类型数组

    boolean add(E e);//增加一个元素

    boolean remove(Object o);//移除一个元素

    boolean containsAll(Collection c);//是否包含指定集合中的所有元素

    boolean addAll(Collection c);//将结合中的所有元素加入到set中

    boolean retainAll(Collection c);//仅保留set中那些包含在指定collection中的元素。

    boolean removeAll(Collection c);//将结合中的所有元素从set中移除

    void clear();//清空

    boolean equals(Object o);//重写equals方法

    int hashCode();//从写hashCode方法
}

public class HashSet
    extends AbstractSet
    implements Set, Cloneable, java.io.Serializable
{
    static final long serialVersionUID = -5024744406713321676L;

    private transient HashMap map;//内部通过map的key来保存set的元素

    private static final Object PRESENT = new Object();//map的value,

    public HashSet() {
	map = new HashMap();//构建一个map,其中value指定为Object
    }

    public HashSet(Collection c) {
	map = new HashMap(Math.max((int) (c.size()/.75f) + 1, 16));
	addAll(c);//将集合c中的元素加入到map中
    }

    public HashSet(int initialCapacity, float loadFactor) {
    //通过指定大小和载入因子创建map
	map = new HashMap(initialCapacity, loadFactor);
    }

    public HashSet(int initialCapacity) {
    //通过指定大小创建map
	map = new HashMap(initialCapacity);
    }

    HashSet(int initialCapacity, float loadFactor, boolean dummy) {//当构造方法有第三个形参时表示这个map是一个LinkedHashMap
	map = new LinkedHashMap(initialCapacity, loadFactor);
    }

    public Iterator iterator() {//返回map中的key迭代器
	return map.keySet().iterator();
    }

    public int size() {//返回map的大小
	return map.size();
    }

    public boolean isEmpty() {//返回map是否为空
	return map.isEmpty();
    }

    public boolean contains(Object o) {//返回map是否包含指定的key
	return map.containsKey(o);
    }

    public boolean add(E e) {//往map中加入一元素
	return map.put(e, PRESENT)==null;
    }

    public boolean remove(Object o) {//移除一个元素
	return map.remove(o)==PRESENT;
    }

    public void clear() {//清空
	map.clear();
    }

    public Object clone() {
    //克隆,以克隆map方式实现
	try {
	    HashSet newSet = (HashSet) super.clone();
	    newSet.map = (HashMap) map.clone();
	    return newSet;
	} catch (CloneNotSupportedException e) {
	    throw new InternalError();
	}
    }
    //写对象
    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());
    }
    //读对象
    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(capacity, loadFactor) :
               new HashMap(capacity, loadFactor));

        // Read in size
        int size = s.readInt();

	// Read in all elements in the proper order.
	for (int i=0; i

你可能感兴趣的:(java)