Depth understanding of Java -List collection framework and a set of selection

List

List implements Collection, so he has all the methods of Collection

List of storage as follows
Here Insert Picture Description

Use the code shown below

//使用List的接口
List<String> list = new ArrayList<>();//创建ArrayList的实现类
//List<String> list = new LinkedList<>();//创建LinkedList的实现类

//添加元素
list.add("元素1");
list.add("元素2");
list.add("元素3");
list.add("元素4");
list.add("元素5");

//打印输出集合以及下标为2的元素
System.out.println(list);
System.out.println("下标为2:"+ list.get(2));

//删除下标为2的元素
list.remove(2);

//打印输出集合以及下标为2的元素
System.out.println(list);
System.out.println("下标为2:"+ list.get(2)); 复制代码

Output

[元素1, 元素2, 元素3, 元素4, 元素5]
下标为2:元素3
[元素1, 元素2, 元素4, 元素5]
下标为2:元素4 复制代码

When to use List

  • Want to store a set of elements
  • These elements may be repeated

How to Select List

There are two commonly used

ArrayList

¨è¿ å ?? æ ?? é ???? ???? ?? å ¥ å ?? æ ???? ???? ¾ç è¿ °

  • Internal maintain an array
  • Every element of the increase or decrease of an element of an array must be re-created
  • Increase and delete large overhead
  • Query and modify the speed

LinkedList

¨è¿ å ?? æ ?? é ???? ???? ?? å ¥ å ?? æ ???? ???? ¾ç è¿ °

  • LinkedList is a doubly linked list,
  • Adding and deleting has a better performance than the ArrayList elements.
  • But elements of the query and modify the terms of weaker than ArrayList.

List of parsing the source code

Collection method of addition, LIst there are several specific methods

Generally speaking, if we use the collection, only need to use the interface on the line, no need to use objects that implement class operation
so only description of the methods described herein List

void replaceAll(UnaryOperator operator)复制代码

Each element of this list to replace the element after calculation results. Operation or error caused by a runtime exception will be thrown to the caller.

Parameters:
operator - applied to each element
Throws:
UnsupportedOperationException - if this list is not modifiable. Implementations may Raised if an element can not be replaced or, if in general, can not be modified

NullPointerException - if designated by the operator is empty or if the result of the operator is a null value, this list does not permit null elements and
internal code:

default void replaceAll(UnaryOperator<E> operator) {
Objects.requireNonNull(operator);
    final ListIterator<E> li = this.listIterator();
    while (li.hasNext()) {
        li.set(operator.apply(li.next()));
    }
} 复制代码

void sort(Comparator c)复制代码

Sequentially generated according to a predetermined ordered list Comparator. Sort is stable: this method can not reorder equal elements.

All the elements in this list are specified comparison (ie, are compared with each other c.compare (e1, e2) must not throw a ClassCastException any elements e1 and e2 in the list) must be used.

If the comparison specified is null then all the elements in this list must implement the Comparable interface and the natural order of elements should be used.
This list must be modifiable, but need not be resizable.

Parameters:
c - receiving Comparator to compare list elements. A null value represents the natural order of the elements should be used
Throws:
ClassCastException - if the list contains the specified comparator can not be compared with each other elements UnsupportedOperationException - if the list is a list iterator does not support the set operation IllegalArgumentException - If the comparison finds a violation Comparator rule

Internal code:

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);
    }
} 复制代码

E get(int index)复制代码

index is a number between 0 ~ Integer.MAX_VALUE

Returns the element at the specified position in this list.

Parameters:
index-index of the element
Returns:
the element specified position in this list

Throws:

IndexOutOfBoundsException - if the index is out of range (index <0 || index> = size ())

E set(int index, E element)复制代码

Replace the specified target element at the List

Parameters:
index - index of the element to replace the
element - the element is stored in a specified location
Returns:
the element before the specified position
Throws:
an UnsupportedOperationException - if the set operation is not supported in this list
ClassCastException - if the class of the specified element to prevent it is added to the list
NullPointerException - if the specified element is null, and this list does not permit null elements
IllegalArgumentException - if it prevents certain properties have been added to this list from the specified element
IndexOutOfBoundsException - if the index is out of range (index < 0 || index> = size () )

void add(int index, E element)复制代码

The specified target position in the insert element, and the elements of the original moving a unified index rearwardly
Parameters:
index - index will be inserted into the element specified
element - elements to be inserted
Throws:
an UnsupportedOperationException - if the add operation is not this list supports
ClassCastException - if the class of the specified element prevents it from being added to the list
NullPointerException - if the specified element is null, and this list does not permit null elements
IllegalArgumentException - if the specified element prevents some of its properties are added this list
IndexOutOfBoundsException - if the index is out of range (index <0 || index> size ())

E remove(int index)复制代码

index is a number between 0 ~ Integer.MAX_VALUE
delete this list to specify the position of the element. The rear element is moved to the left. Return element from being deleted.
Parameters:
index - index
Returns:
removed element
Throws:
an UnsupportedOperationException - if the remove operation is not supported in this list
IndexOutOfBoundsException - if the index is out of range (index <0 || index> = size ())

int indexOf(Object o)复制代码


O looking element in the List, the element will return the first time in the index List
not found -1
Parameters:
o - element to search for
Returns: the
specified element first appeared in the list of indexes, can not find -1 to

int lastIndexOf(Object o)复制代码

Returns the last element of the index in this list of the first occurrence, or -1 if this list does not contain the elements. More formally, returns i like this Objects.equals (o, get (i)), or -1 if no such indicators.

Function:
O - element to search for
Returns:
the specified element, the last occurrence in this list of the index, or -1 if this list does not contain the elements
Throws:
ClassCastException - if the type of the specified element is incompatible with this list
NullPointerException - if the specified element is null, and this list does not permit null elements

ListIterator listIterator()复制代码

Returns a list iterator

Returns:
Returns a list iterator

ListIterator listIterator(int index)复制代码

Returns a list iterator starting from a specified index

Parameters:
index of the first element of the index- iterator
Returns:
Returns the specified index from a start list iterator
Throws:
an IndexOutOfBoundsException - if the index is out of range (index <0 || index> size ())

List subList(int fromIndex, int toIndex)复制代码

Interception List

List<Object> subList = list.subList(0, 5);
//其中subList(0, 5)取得的是下标为0到4的元素,不包含下标为5的元素. 复制代码

Parameters:
fromIndex - starting
toIndex - ending point
Returns:
the designated range within the list view
Throws:
an IndexOutOfBoundsException - index value is an invalid endpoint (fromIndex <0 || toIndex> size || fromIndex> toIndex)

static  List of(E e1)复制代码

Returns a list containing one element can not be changed. See the detailed information is not available to modify the list.

Parameters:
E1 - the single member, there may be a plurality, or may not return an empty write
type parameter:
<E> - List of element type
Returns:
a List contains the specified elements
Throws:
a NullPointerException - If the element is null

static  List copyOf(Collection coll)复制代码

Collection copy a set of sequential order iterator
parameters:
Coll - Collection set a
type parameter:
<E> - List of element type
Returns:
a given element comprising List Collection
Throws:
a NullPointerException - if null, or If it contains any blank


Guess you like

Origin juejin.im/post/5e469475f265da574064166a