小編給大家分享一下java中LinkedList使用迭代器優化移除批量元素原理分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
具體如下:
public interface Iterator<E> {
/**
*是否還有下一個元素
*/
boolean hasNext();
/**
*下一個元素
*/
E next();
/**
* 從集合中刪除最后一個返回的元素
*/
default void remove() {
throw new UnsupportedOperationException("remove");
}
/**
* 傳入一個Consumer對剩余的每個元素執行指定的操作,直到所有元素已處理或操作引發異常
*/
default void forEachRemaining(Consumer<? super E> action) {
//requireNonNull 靜態方法將會在參數為null時主動拋出NullPointerException 異常。
Objects.requireNonNull(action);
while (hasNext())
action.accept(next());
}
}public interface ListIterator<E> extends Iterator<E> {
/**
* 是否有后繼
*/
boolean hasNext();
/**
* 后繼節點
*/
E next();
/**
* 是否有前驅
*/
boolean hasPrevious();
/**
* 前驅節點
*/
E previous();
/**
* 下一個節點的下標
*/
int nextIndex();
/**
* 前驅節點的下標
*/
int previousIndex();
/**
* 移除元素
*/
void remove();
/**
* 替換最后返回的元素
*/
void set(E e);
/**
* 插入一個結點到最后返回的元素之后
*/
void add(E e);
}調用方法:list.iterator();
public Iterator<E> iterator() {
return new Itr();
}private class Itr implements Iterator<E> {
int cursor; // 當前下標
int lastRet = -1; // 最后一個元素的索引,沒有返回1
int expectedModCount = modCount;//創建迭代器時列表被修改的次數,防止多線程操作??焖偈?
/**
* 先檢查一下是否列表已經被修改過
* 做一些簡單的越界檢查
* 返回元素,并且記錄下返回元素的下標給lastRet,當前下標+1,
*/
public E next() {
checkForComodification();
int i = cursor;
if (i >= size)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i + 1;
return (E) elementData[lastRet = i];
}
/**
* 調用ArrayListdie自身的remove方法移除元素
* ArrayListdie自身的remove 會修改modCount的值所以需要更新迭代器的expectedModCount
* expectedModCount = modCount;
*/
public void remove() {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.remove(lastRet);
cursor = lastRet;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
//把剩下為next遍歷的所有元素做一個遍歷消費
@Override
@SuppressWarnings("unchecked")
public void forEachRemaining(Consumer<? super E> consumer) {
Objects.requireNonNull(consumer);
final int size = ArrayList.this.size;
int i = cursor;
if (i >= size) {
return;
}
final Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length) {
throw new ConcurrentModificationException();
}
while (i != size && modCount == expectedModCount) {
consumer.accept((E) elementData[i++]);
}
// update once at end of iteration to reduce heap write traffic
cursor = i;
lastRet = i - 1;
checkForComodification();
}
//檢查是否列表已經被修改了
final void checkForComodification() {
if (modCount != expectedModCount)
throw new ConcurrentModificationException();
}
}
實現了ListIterator接口,ListIterator接口繼承Iterator接口。并且ListItr extends Itr。Itr有的方法其都有。并且增加了
hasPrevious 是否有前驅
nextIndex 下一個索引位置
previousIndex 前驅的索引位置
previous 前驅元素
set 替換最后返回的元素
add 插入一個結點到最后返回的元素之后
private class ListItr extends Itr implements ListIterator<E> {
ListItr(int index) {
super();
cursor = index;
}
public boolean hasPrevious() {
return cursor != 0;
}
public int nextIndex() {
return cursor;
}
public int previousIndex() {
return cursor - 1;
}
public E previous() {
checkForComodification();
int i = cursor - 1;
if (i < 0)
throw new NoSuchElementException();
Object[] elementData = ArrayList.this.elementData;
if (i >= elementData.length)
throw new ConcurrentModificationException();
cursor = i;
return (E) elementData[lastRet = i];
}
//根據lastRet設置元素
public void set(E e) {
if (lastRet < 0)
throw new IllegalStateException();
checkForComodification();
try {
ArrayList.this.set(lastRet, e);
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
public void add(E e) {
checkForComodification();
try {
int i = cursor;
ArrayList.this.add(i, e);
cursor = i + 1;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException ex) {
throw new ConcurrentModificationException();
}
}
}調用方法:list.iterator();

重點看下remove方法
private class ListItr implements ListIterator<E> {
//返回的節點
private Node<E> lastReturned;
//下一個節點
private Node<E> next;
//下一個節點索引
private int nextIndex;
//修改次數
private int expectedModCount = modCount;
ListItr(int index) {
//根據傳進來的數字設置next等屬性,默認傳0
next = (index == size) ? null : node(index);
nextIndex = index;
}
//直接調用節點的后繼指針
public E next() {
checkForComodification();
if (!hasNext())
throw new NoSuchElementException();
lastReturned = next;
next = next.next;
nextIndex++;
return lastReturned.item;
}
//返回節點的前驅
public E previous() {
checkForComodification();
if (!hasPrevious())
throw new NoSuchElementException();
lastReturned = next = (next == null) ? last : next.prev;
nextIndex--;
return lastReturned.item;
}
/**
* 最重要的方法,在LinkedList中按一定規則移除大量元素時用這個方法
* 為什么會比list.remove效率高呢;
*/
public void remove() {
checkForComodification();
if (lastReturned == null)
throw new IllegalStateException();
Node<E> lastNext = lastReturned.next;
unlink(lastReturned);
if (next == lastReturned)
next = lastNext;
else
nextIndex--;
lastReturned = null;
expectedModCount++;
}
public void set(E e) {
if (lastReturned == null)
throw new IllegalStateException();
checkForComodification();
lastReturned.item = e;
}
public void add(E e) {
checkForComodification();
lastReturned = null;
if (next == null)
linkLast(e);
else
linkBefore(e, next);
nextIndex++;
expectedModCount++;
}
}LinkedList 源碼的remove(int index)的過程是
先逐一移動指針,再找到要移除的Node,最后再修改這個Node前驅后繼等移除Node。如果有批量元素要按規則移除的話這么做時間復雜度O(n²)。但是使用迭代器是O(n)。
LinkedList是雙向鏈表,這里示意圖簡單畫個單鏈表
比如要移除鏈表中偶數元素,先循環調用get方法,指針逐漸后移獲得元素,比如獲得index = 1;指針后移兩次才能獲得元素。
當發現元素值為偶數是。使用idnex移除元素,如list.remove(1);鏈表先Node node(int index)返回該index下的元素,與get方法一樣。然后再做前驅后繼的修改。所以在remove之前相當于做了兩次get請求。導致時間復雜度是O(n)。


繼續移除下一個元素需要重新再走一遍鏈表(步驟忽略當index大于半數,鏈表倒序查找)

以上如果移除偶數指針做了6次移動。
刪除2節點
get請求移動1次,remove(1)移動1次。
刪除4節點
get請求移動2次,remove(2)移動2次。
迭代器的next指針執行一次一直向后移動的操作。一共只需要移動4次。當元素越多時這個差距會越明顯。整體上移除批量元素是O(n),而使用list.remove(index)移除批量元素是O(n²)

以上是“java中LinkedList使用迭代器優化移除批量元素原理分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。