集合(Collection(Set))

|Collection
   ||List
   ||Set
   ||Queue

我们来看看Set的JDK,因为它继承Collection,我们主要关心它和Collection的区别(相同的部分就略过不写注释,可参照Collection):

//We can know that Set is interface and extends Collection
public interface Set extends Collection {
///// Query Operations
   int size();
   boolean isEmpty();
   boolean contains(Object o);
   Iterator iterator();
   Object[] toArray();
    T[] toArray(T[] a);
  
///// Modification Operations
   //Return true if added, the same element can't be added to a Set
   //Also you can add a null element but only once
   boolean add(E e);

   boolean remove(Object o);

///// Bulk Operations
   boolean containsAll(Collection c);
   boolean retainAll(Collection c);
   boolean removeAll(Collection c);
   void clear();

   //Add all the elements of c which are not contained in this Set
   //Return true if added  
   boolean addAll(Collection c);

///// Comparison and hashing
   boolean equals(Object o);
   int hashCode();
   default Spliterator spliterator() {
}

Set在Collection的基础上加了一点定义。Set,从数学上讲也是不允许重复元素的,而且可以包含null,即空元素。所以对于Set, add操作是不能添加已有的元素的。。

你可能感兴趣的:(集合(Collection(Set)))