Java集合框架5Set

Set概述

Set几乎都是内部用一个Map来实现, 因为Map里的KeySet就是一个Set,而value是假值,全部使用同一个Object。Set的特征也继承了那些内部Map实现的特征。

HashSet

  • 1 以jdk7为准进行说明
package java.util;
public class HashSet
    extends AbstractSet
    implements Set, Cloneable, java.io.Serializable
{
    private transient HashMap map;
    private static final Object PRESENT = new Object();
    public HashSet() {
        map = new HashMap<>();
    }
//其余省略
}
  • 2 HashSet是基于HashMap来实现的,操作很简单,更像是对HashMap做了一次“封装”,而且只使用了HashMap的key来实现各种特性,而HashMap的value始终都是PRESENT。
  • 3 HashSet不允许重复(HashMap的key不允许重复,如果出现重复就覆盖),允许null值,非线程安全。
  • 4 主要的几个方法如下
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);
    }
    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();
    }
  • 5 HashSet有4个共有的构造函数:
    public HashSet() {
        map = new HashMap<>();
    }
   public HashSet(Collection c) {
        map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
        addAll(c);
    }
    public HashSet(int initialCapacity, float loadFactor) {
        map = new HashMap<>(initialCapacity, loadFactor);
    }
    public HashSet(int initialCapacity) {
        map = new HashMap<>(initialCapacity);
    }
   HashSet(int initialCapacity, float loadFactor, boolean dummy) {
        map = new LinkedHashMap<>(initialCapacity, loadFactor);
    }

LinkedHashSet

  • 1 以jdk7为准进行说明
package java.util;
public class LinkedHashSet
    extends HashSet
    implements Set, Cloneable, java.io.Serializable {
    private static final long serialVersionUID = -2851667679971038690L;

    public LinkedHashSet(int initialCapacity, float loadFactor) {
        super(initialCapacity, loadFactor, true);
    }

    public LinkedHashSet(int initialCapacity) {
        super(initialCapacity, .75f, true);
    }

    public LinkedHashSet() {
        super(16, .75f, true);
    }

    public LinkedHashSet(Collection c) {
        super(Math.max(2*c.size(), 11), .75f, true);
        addAll(c);
    }
}
  • 2 整个LinkedHashSet的四个构造函数全部都是调用了HashSet中的这个包级私有的构造函数(构造函数中的dummy就是标记,无实用,为了实现方法的重载而已),也就是说LinkedHashSet就是基于LinkedHashMap实现的。

TreeSet

  • 1 以jdk7为准进行说明
package java.util;
public class TreeSet extends AbstractSet
    implements NavigableSet, Cloneable, java.io.Serializable
{
    private transient NavigableMap m;
    private static final Object PRESENT = new Object();
    public TreeSet() {
        this(new TreeMap());
    }
    public TreeSet(Comparator comparator) {
        this(new TreeMap<>(comparator));
    }
    public TreeSet(Collection c) {
        this();
        addAll(c);
    }
    public TreeSet(SortedSet s) {
        this(s.comparator());
        addAll(s);
    }
//其余省略
}
  • 2 TreeSet的内部基于TreeMap实现,同样value永远为PRESENT.

你可能感兴趣的:(Java集合框架5Set)