美文网首页
集合(Collection(Set))

集合(Collection(Set))

作者: CelloRen | 来源:发表于2018-03-26 00:17 被阅读0次
    |Collection
       ||List
       ||Set
       ||Queue
    

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

    //We can know that Set is interface and extends Collection
    public interface Set<E> extends Collection<E> {
    ///// Query Operations
       int size();
       boolean isEmpty();
       boolean contains(Object o);
       Iterator<E> iterator();
       Object[] toArray();
       <T> 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<? extends E> c);
    
    ///// Comparison and hashing
       boolean equals(Object o);
       int hashCode();
       default Spliterator<E> spliterator() {
    }
    

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

    相关文章

      网友评论

          本文标题:集合(Collection(Set))

          本文链接:https://www.haomeiwen.com/subject/qpgccftx.html