集合(Collection(List))

|Collection
   ||List
   ||Set
   ||Queue

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

//We can know List is interface and extends Collection
public interface List<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, appends the element to the end of this list
   boolean add(E e);
   
   //Return true if removed, remove the first occurrence from this list
   boolean remove(Object o);

///// Bulk Modification Operations
   boolean containsAll(Collection<?> c);

   //Return true if all added, appends all the elements of c to this list 
   boolean addAll(Collection<? extends E> c);
   
   //Return true if all added, insert all the elements of c to this list at index
   boolean addAll(int index, Collection<? extends E> c);

      /**In my view, the two operations is hard to achieve based on LinkedList**/
   boolean removeAll(Collection<?> c);
   boolean retainAll(Collection<?> c);

   default void replaceAll(UnaryOperator<E> operator) {}

   //Sort the list according to the comparator c
   default void sort(Comparator<? super E> c) {
        Object[] a = this.toArray();
        Arrays.sort(a, (Comparator) c);
        ListIterator<E> i = this.listIterator();
        for (Object e : a) {
            i.next();
            i.set((E) e);
        }
    }

    void clear();

///// Comparison and hashing
   boolean equals(Object o);
   int hashCode();

///// Positional Access Operations
   //Return the element at index position
   E get(int index);
   
   //Replace the element at index position with the new element, return the previous
   E set(int index, E element);

   //Insert the element at index
   void add(int index, E element);

   //Return the element removed, remove the element at index 
   E remove(int index);
///// Search Operations
   //Return the index of first occurrence that is equal to 0,
   // return -1 if there is no such an element
   int indexOf(Object o);

   //Return the index of last occurrence that is equal to 0,
   // return -1 if there is no such an element
   int indexOf(Object o);

///// List Iterators
   ListIterator<E> listIterator();
   ListIterator<E> listIterator(int index);
}

 ///// View
    List<E> subList(int fromIndex, int toIndex);
  default Spliterator<E> spliterator() {
}

List的实现基础如果是链表,一些方法实现很麻烦,像这两个操作:

boolean removeAll(Collection<?> c);
boolean retainAll(Collection<?> c);

从一个链表删除,它和另一个链表的交集,如果是无序的话,时间复杂度很高;
同理,保留交集,删除其他的, 也麻烦。
所以LinkedList源码里没有这两个方法。
由此也可以初步地引出ArrayList和LinkedList的区别:一个的底层是数组,一个的底层是链表

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、基础知识:1、JVM、JRE和JDK的区别:JVM(Java Virtual Machine):java虚拟机...
    杀小贼阅读 2,606评论 0 4
  • Java集合 作为一个Developer,Java集合类是我们在工作中运用最多的、最频繁的类。相比于数组(Arra...
    贾博岩阅读 66,049评论 14 103
  • 又是春来雨无常 鸟城深夜影彷徨 人来人往归寂寞 大指小指落键盘
    庸行者SZ阅读 262评论 0 0
  • 终于有一天青春过去,我们长大四年前听的歌四年后物是人非 小时候觉得小说中的故事波诡云谲充满了戏剧性长大以后发现生活...
    d84fa956e67b阅读 189评论 0 0
  • 大三的自己很喜欢那些画面细腻美好并且意蕴悠长的动漫,所以自己是冥冥中注定与《虫师》相遇,并且深深地爱上它的。 现如...
    十亩之间兮阅读 177评论 0 0

友情链接更多精彩内容