[Java Collections Framework] 浅析java 集合框架(二) : 线性结构(List,Queue,Deque)

本篇文章总结自 Java Collections Framework Tutorial interface

Collection Interface

这里写图片描述

基本操作

int size(), boolean isEmpty(), boolean contains(Object element), boolean add(E element), boolean remove(Object element), and Iterator<E> iterator().

像他的名字一样,看了就懂 注意 只有增删(add,remove),没有改和查(set,get)

遍历集合

官方说明中给了3种方式:

There are three ways to traverse collections: (1) using aggregate operations (2) with the for-each construct and (3) by using Iterator s.

PS: 很显然,它的意思是尽量按照1,2,3的顺序选择,看来是时候学习 JDK 8了

  1. aggregation operation: 这种方式用JDK 8 中 Stream 的方式工作,不会修改集合,可以做很多,filter之类的事情
  2. for-each: for(Object o : C){/* do something*/}
  3. Iterator:
    官方说仅在两种情况下需要使用它:

    • Remove the current element. The for-each construct hides the iterator, so you cannot call remove. Therefore, the for-each construct is not usable for filtering.

    • Iterate over multiple collections in parallel.

    比如说需要删除集合中的元素的时候

public static void filter(Collection<Integer> c) {
        for (Iterator<Integer> it = c.iterator(); it.hasNext(); )
            if (!cond(it.next()))
                it.remove();//会修改集合
    }

批处理

就是那一堆带All的函数

  • containsAll — returns true if the target Collection contains all of the elements in the specified Collection.
  • addAll — adds all of the elements in the specified Collection to the target Collection.
  • removeAll — removes from the target Collection all of its elements that are also contained in the specified Collection.
  • retainAll — removes from the target Collection all its elements that are not also contained in the specified Collection. That is, it retains only those elements in the target Collection that are also contained in the specified Collection.
  • clear — removes all elements from the Collection.

数组操作

toArray提供了到Collection到Array的桥梁。

两个方法 :

  • Object[] toArray() 不知道具体元素类型时用
  • <T> T[] toArray(T[] a) 知道具体类型时

后一种在数组大小满足集合元素的情况下不会创建新数组,即返回的数组和传进去的参数是一样的,并且把多余的位置填为null,否则不改变原数组,创建新数组返回。通常的用法是

String[] a = c.toArray(new String[0]);

        ArrayList<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,-3));
        Integer[] a = new Integer[list.size()+1];
        Integer[] listArray = list.toArray(a);
        System.out.println(a==listArray);// a.length() >= a.size(),return true      
        for(int i=0 ; i<a.length ; ++i)//多余的元素修改为null
            System.out.println(a[i]);

List

这里写图片描述

List是一种线性结构的表,继承自 Collection,通常的实现有LinkedList和ArrayList,(注意 vector 虽然也是一种实现但由于加入了线程安全性,所以通常效率不高,应该是历史遗留原因,C++的朋友如要使用Vector推荐使用ArrayList。)我们只需要看看它扩展的接口就好,(见上图标注)

官方文档说的明明白白,由于是线性结构,可以基于位置访问,所以扩展了4点:

  • Positional access — manipulates elements based on their numerical position in the list. This includes methods such as get, set, add, addAll, and remove.
  • Search — searches for a specified object in the list and returns its numerical position. Search methods include indexOf and lastIndexOf.
  • Iteration — extends Iterator semantics to take advantage of the list’s sequential nature. The listIterator methods provide this behavior.
  • Range-view — The sublist method performs arbitrary range operations on the list.

其他几项都很好明白,这里说一下迭代器这个,listIterator,提供了一个基于位置设置的迭代器,并且可以指向前,也可以向后。官方文档给了几个例子,可以看看,不过依旧需要注意的是,只有在for-each满足不了你的需求,也就是需要增删改查的时候,采用迭代器,用的时候最好直接用listIterator.

Queue

这里写图片描述
提供了查询队首元素的操作(e.g.: element(),peek())

他的增删查都做了2组操作,1) 操作失败抛异常,(e.g.: 队列为空,内存不够 ) 2) 返回特殊值,null,false

这里写图片描述

通常的实现是FIFO,(LinkedList,ArrayQueue), 优先队列有其自己的实现方式

Deque

这里写图片描述

双端队列,看他的方法,核心的就是扩充 Queue在head和Tail的实现,即两端增删查
这里写图片描述

不过由于java Collection Framework里面没有Stack的专门实现(Stack,继承自vector,历史原因,线程安全牺牲效率),这里就封装了,push和pop。用于栈操作,通常情况下(不带First,last)的操作与Queue相同,是个Fifo

  • FIFO: add,offer,poll,remove,peek,element
  • LIFO: push,pop

总结

这节主要是看UML和官方文档对List,Queue,Deque这几个接口做了一点总结,源码没有看的价值。回到第一节的UML图

这里写图片描述

第二层就是一些abstract 开头的类,这些抽象类主要是为了 Custom Collection Implementations,做准备的,也就是给那些需要自己实现自己的List,Queue之类的人做准备的,所以这层我就不看了,而是直接开始从底层看最终的实现。

这里写图片描述

猜你喜欢

转载自blog.csdn.net/Dylan_Frank/article/details/81210670