java中LinkedList使用迭代器優(yōu)化移除批量元素原理
本文主要介紹了java中LinkedList使用迭代器優(yōu)化移除批量元素原理,分享給大家,具體如下:
public interface Iterator<E> {
/**
*是否還有下一個元素
*/
boolean hasNext();
/**
*下一個元素
*/
E next();
/**
* 從集合中刪除最后一個返回的元素
*/
default void remove() {
throw new UnsupportedOperationException("remove");
}
/**
* 傳入一個Consumer對剩余的每個元素執(zhí)行指定的操作,直到所有元素已處理或操作引發(fā)異常
*/
default void forEachRemaining(Consumer<? super E> action) {
//requireNonNull 靜態(tài)方法將會在參數為null時主動拋出NullPointerException 異常。
Objects.requireNonNull(action);
while (hasNext())
action.accept(next());
}
}
public interface ListIterator<E> extends Iterator<E> {
/**
* 是否有后繼
*/
boolean hasNext();
/**
* 后繼節(jié)點
*/
E next();
/**
* 是否有前驅
*/
boolean hasPrevious();
/**
* 前驅節(jié)點
*/
E previous();
/**
* 下一個節(jié)點的下標
*/
int nextIndex();
/**
* 前驅節(jié)點的下標
*/
int previousIndex();
/**
* 移除元素
*/
void remove();
/**
* 替換最后返回的元素
*/
void set(E e);
/**
* 插入一個結點到最后返回的元素之后
*/
void add(E e);
}
普通版 ArrayListdie迭代器
調用方法:list.iterator();
public Iterator<E> iterator() {
return new Itr();
}
private class Itr implements Iterator<E> {
int cursor; // 當前下標
int lastRet = -1; // 最后一個元素的索引,沒有返回1
int expectedModCount = modCount;//創(chuàng)建迭代器時列表被修改的次數,防止多線程操作??焖偈?
/**
* 先檢查一下是否列表已經被修改過
* 做一些簡單的越界檢查
* 返回元素,并且記錄下返回元素的下標給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();
}
}
增強版本 ArrayListdie迭代器

實現了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();
}
}
}
重點看一下LinkedList的迭代器
調用方法:list.iterator();

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


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

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

到此這篇關于java中LinkedList使用迭代器優(yōu)化移除批量元素原理的文章就介紹到這了,更多相關LinkedList 迭代器批量移除內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
解決IDEA創(chuàng)建第一個spring boot項目提示cannot resolve xxx等
這篇文章主要介紹了解決IDEA創(chuàng)建第一個spring boot項目提示cannot resolve xxx等錯誤問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-01-01
基于java servlet過濾器和監(jiān)聽器(詳解)
下面小編就為大家?guī)硪黄趈ava servlet過濾器和監(jiān)聽器(詳解)。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10

