List of Collection

The source code of all the screenshots here comes from JDK8

see a picture


First look at the basic interface of the collection class: Collection, which inherits the Iterable interface.


When looking at the source code, I found that there is a method body under this interface,


This is due to the new features in JDK8: interfaces allow to define non-abstract methods, but they must be decorated with default.

There are two basic methods in Collection

 
 
boolean add(E e);跟Iterator<E> iterator();

add is to add elements to the collection, return true if successful, and false if failed (but duplicate objects are not allowed in this collection)

The iterator method is used to return an object of the Iterator interface


There are three well-known methods in this interface object: hasNext, next, and remove.

The next method allows us to access each element in the collection one by one, but if we reach the end of the collection, the next method will have a NoSuchElementException exception, so we need to call the hasNext method, and when hasNext returns true, call next repeatedly to implement the collection. Traversal (here we can also know that the way arrays access elements is using subscripts, while collections use iterators). Let's take a look at the method Itr of an inner class in ArrayList that implements the Iterator interface


If i>=size, the end of the set has been reached.

ConcurrentModificationException may be because the external remove method modifies the value of modCount , which is the value of expectedModCount
An inconsistency has occurred.

There is such a sentence in the Java core technology volume: Java iterator search operations are closely linked when the position changes, and the only way to find an element is to call next,
While the search operation is performed, the position of the iterator will also move forward, so the iterator should be considered to be between two elements. When calling next, iterate
The handler will step over to the next element and return a reference to the over-stepped element.
The remove method of the Iterator interface removes the element returned by the last call to the next method. So: if you delete two connected elements, we need to call next to return the need to delete
Object.
The specific set is described below:
So far, the most used ArrayList class (index sequence that can grow and shrink dynamically)
It inherits the AbstractList class, which in turn inherits the AbstractCollection class, because Collection has many methods, but if each class has
Implementing so many methods loses its original meaning, so provide an abstract class to better implement the Collection interface.
The disadvantage of ArrayList can also be said to be a defect of the array: the deletion operation requires a huge price, because the elements after deletion
Both need to be moved forward, and insert will have the same problem. But the linked list does not have this problem, because each node in the linked list is doubly linked (the array is in consecutive bits
It is precisely because the node stores the reference of the predecessor node, so it is very simple to delete and insert in the linked list, because only need to update the nearby
A reference to the node is sufficient .
 
The book says: But the linked list is an ordered collection, and the position of each object is very important, so,
The LinkedList.add method inserts the value at the end of the linked list each time:

   


  From the linkLast method, you can see that the new node is linked together at the last node every time. If last == null, it is used as the first node.
  So how to insert elements into the middle of the linked list, use the iterator (ListIterator), because the iterator is used to despise the position in the collection, so this depends on the position
  The add method of the iterator should be the responsibility of the iterator
  There is a ListIterator method under the List interface.

  

 
 
In this way, we can implement the operation of inserting in the middle of linkedList.

     But it is actually:

    

我发现LinkedList中add方法包含了根据下标(可能下标不太准确)去增加value值的方法、那就来看一下源码:


就是如果插入位置是在末尾,那就直接调用linkLast方法进行末尾插入,但是如果不是末尾,那么我们就需要去执行一些操作:

首先看node(index):if跟else的判断是遍历方式从头到尾或者从尾到头。


 再根据Node位置进行插入操作(其实质感觉还是依赖于下标的位置)


 LinkedList的缺点:对于随机访问遍历链表效率很低:因为每次查找一个元素都需要从列表头部开始搜索。LinkedList对象

根本不做任何缓存位置信息的操作。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324920031&siteId=291194637