介绍

Set接口是Collection的子接口,set接口没有提供额外的方法。

Set集合元素无序且不允许包含相同的元素,如果试把两个相同的元素加入同一个Set集合中,则添加操作失败。

  • 无序性:不等于随机性。存储的数据在底层数据中并非按照数据索引的顺序添加,而是根据数据的哈希值决定的

  • 不可重复性:保证添加的元素按照equals()判断时,不能返回ture,即相同的元素只能添加一个

Set判断两个对象是否相同不是使用==运算符,而是根据equals()方法。

HashSet

HashSet是Set接口的典型实现,大多数时候使用Set集合时都使用这个实现类。HashSet按Hash算法来存储集合中的元素,因此具有很好的存取、查找、删除性能。

HashSet具有以下特点:

  • 不能保证元素的排列顺序

  • HashSet不是线程安全的

  • 集合元素可以是null

HashSet集合判断两个元素相等的标准两个对象通过hashCode()方法比较相等,并且两个对象的equals()方法返回值也相等

对于存放在Set容器中的对象,对应的类一定要重写equals()hashCode(Object obj)方法,以实现对象相等规则。即:“相等的对象必须具有相等的散列码”。

JDK7&JDK8

HashSet在JDK7和JDK8中的区别在于HashMap在两个JDK之间的区别。

底层使用HashMap

 private transient HashMap<E,Object> map;

构造器

 /**
  * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
  * default initial capacity (16) and load factor (0.75).
  */
 public HashSet() {
     map = new HashMap<>();
 }
 ​
 /**
  * Constructs a new set containing the elements in the specified
  * collection.  The <tt>HashMap</tt> is created with default load factor
  * (0.75) and an initial capacity sufficient to contain the elements in
  * the specified collection.
  *
  * @param c the collection whose elements are to be placed into this set
  * @throws NullPointerException if the specified collection is null
  */
 public HashSet(Collection<? extends E> c) {
     map = new HashMap<>(Math.max((int) (c.size()/.75f) + 1, 16));
     addAll(c);
 }
 ​
 /**
  * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
  * the specified initial capacity and the specified load factor.
  *
  * @param      initialCapacity   the initial capacity of the hash map
  * @param      loadFactor        the load factor of the hash map
  * @throws     IllegalArgumentException if the initial capacity is less
  *             than zero, or if the load factor is nonpositive
  */
 public HashSet(int initialCapacity, float loadFactor) {
     map = new HashMap<>(initialCapacity, loadFactor);
 }
 ​
 /**
  * Constructs a new, empty set; the backing <tt>HashMap</tt> instance has
  * the specified initial capacity and default load factor (0.75).
  *
  * @param      initialCapacity   the initial capacity of the hash table
  * @throws     IllegalArgumentException if the initial capacity is less
  *             than zero
  */
 public HashSet(int initialCapacity) {
     map = new HashMap<>(initialCapacity);
 }
 ​
 /**
  * Constructs a new, empty linked hash set.  (This package private
  * constructor is only used by LinkedHashSet.) The backing
  * HashMap instance is a LinkedHashMap with the specified initial
  * capacity and the specified load factor.
  *
  * @param      initialCapacity   the initial capacity of the hash map
  * @param      loadFactor        the load factor of the hash map
  * @param      dummy             ignored (distinguishes this
  *             constructor from other int, float constructor.)
  * @throws     IllegalArgumentException if the initial capacity is less
  *             than zero, or if the load factor is nonpositive
  */
 HashSet(int initialCapacity, float loadFactor, boolean dummy) {
     map = new LinkedHashMap<>(initialCapacity, loadFactor);
 }

添加

Map的key为我们要添加的元素,value为PRESENT,所以保证了Set的不重复性

 // Dummy value to associate with an Object in the backing Map
 private static final Object PRESENT = new Object();
 ​
 /**
  * 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;
 }

clone

浅拷贝

 /**
  * Returns a shallow copy of this <tt>HashSet</tt> instance: the elements
  * themselves are not cloned.
  *
  * @return a shallow copy of this set
  */
 @SuppressWarnings("unchecked")
 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(e);
     }
 }

TreeSet

TreeSet是SortedSet接口的实现类,TreeSet可以确保集合元素处于排序状态。

向TreeSet中添加的是同类的对象。

TreeSet底层使用红黑树结构存储数据。

TreeSet两种排序方法:自然排序和定制排序。默认情况下,TreeSet采用自然排序。

新增方法

  • Comparator comparator()

  • Object first()

  • Object last()

  • Object lower(Object e)

  • Object higher(Object e)

  • SortedSet subSet(fromElement,toElement)

  • SortedSet headSet(toElement)

  • SortedSet tailSet(fromElement)