Detailed Collection-List Interface

List interface

|----Collection接口:单列集合,用来存储一个一个的对象
         |----List接口:存储有序的、可重复的数据。  -->“动态”数组,替换原有的数组
             |----ArrayList:作为List接口的主要实现类;线程不安全的,效率高;底层使用Object[] elementData存储              		|----LinkedList:对于频繁的插入、删除操作,使用此类效率比ArrayList高;底层使用双向链表存储
    		 |----Vector:作为List接口的古老实现类;线程安全的,效率低;底层使用Object[] elementData存储

In addition to the methods inherited from the Collection collection, List collection adds some methods to manipulate collection elements based on index.

method description
void add(int index, Object ele) Insert the ele element at the index position
boolean addAll(int index, Collection eles) Add all the elements in eles starting from the index position
Object get(int index) Get the element at the specified index position
int indexOf(Object obj) Returns the position where obj first appears in the collection
int lastIndexOf(Object obj) Returns the position of the last occurrence of obj in the current collection
Object remove(int index) Remove the element at the specified index position and return this element
Object set(int index, Object ele) Set the element at the specified index position to ele
List subList(int fromIndex, int toIndex) Returns the sub-collection from fromIndex to toIndex

One of the implementation classes of the List interface: ArrayList

ArrayList is a typical implementation class and main implementation class of the List interface

  • Essentially, ArrayList is a "variable length" array of object references
  • The List collection returned by the Arrays.asList(...) method is neither an ArrayList instance nor a Vector instance. Arrays.asList(…) The return value is a fixed-length List collection

What is the difference between the implementation of ArrayList before and after JDK1.8?

jdk 7:
	ArrayList list = new ArrayList();//底层创建了长度是10的Object[]数组elementData
      	list.add(123);//elementData[0] = new Integer(123);
      	...
      	list.add(11);//如果此次的添加导致底层elementData数组容量不够,则扩容。
      	默认情况下,扩容为原来的容量的1.5倍,同时需要将原有数组中的数据复制到新的数组中。

      	结论:建议开发中使用带参的构造器:ArrayList list = new ArrayList(int capacity)
jdk 8:
	ArrayList list = new ArrayList();//底层Object[] elementData初始化为{}.并没有创建长度为10的数组		
		list.add(123);//第一次调用add()时,底层才创建了长度10的数组,并将数据123添加到elementData[0]
        ...
        后续的添加和扩容操作与jdk 7 无异。

Implementation of the List interface category two: LinkedList

For frequent operations of inserting or deleting elements, it is recommended to use the LinkedList class, which is more efficient

New method:

  • void addFirst (Object obj)
  • void addLast(Object obj)
  • Object getFirst()
  • Object getLast()
  • Object removeFirst()
  • Object removeLast()

LinkedList: Doubly linked list, no array is declared inside, but first and last of the Node type are defined for recording the first and last elements. At the same time, the internal class Node is defined as the basic structure for storing data in LinkedList. In addition to saving data, Node also defines two variables:

  • The prev variable records the position of the previous element
  • The next variable records the position of the next element
private static class Node<E> {
    
    
    E item;
    Node<E> next;
    Node<E> prev;

    Node(Node<E> prev, E element, Node<E> next) {
    
    
        this.item = element;
        this.next = next;
        this.prev = prev;
    }
}

List interface implementation category three: Vector

Vector is an ancient collection, and JDK1.0 has it. Most operations are the same as ArrayList, the difference is that Vector is thread-safe.

Among various lists, it is best to use ArrayList as the default choice. When inserting and deleting frequently, use LinkedList; Vector is always slower than ArrayList, so try to avoid using it.

New method:

  • void addElement(Object obj)
  • void insertElementAt(Object obj,int index)
  • void setElementAt(Object obj,int index)
  • void removeElement(Object obj)
  • void removeAllElements()

Interview questions:

请问ArrayList/LinkedList/Vector的异同?谈谈你的理解?ArrayList底层是什么?扩容机制?Vector和ArrayList的最大区别?
 
ArrayList和LinkedList的异同
二者都线程不安全,相对线程安全的Vector,执行效率高。
此外,ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构。对于随机访问get和set,ArrayList觉得优于LinkedList,因为LinkedList要移动指针。对于新增和删除操作add(特指插入)和remove,LinkedList比较占优势,因为ArrayList要移动数据。

ArrayList和Vector的区别
Vector和ArrayList几乎是完全相同的,唯一的区别在于Vector是同步类(synchronized),属于强同步类。因此开销就比ArrayList要大,访问要慢。正常情况下,大多数的Java程序员使用ArrayList而不是Vector,因为同步完全可以由程序员自己来控制。Vector每次扩容请求其大小的2倍空间,而ArrayList是1.5倍。Vector还有一个子类Stack。

Guess you like

Origin blog.csdn.net/qq_44346427/article/details/110729382