java中LinkedList使用迭代器優(yōu)化移除批量元素原理
本文主要介紹了java中LinkedList使用迭代器優(yōu)化移除批量元素原理,分享給大家,具體如下:
public interface Iterator<E> { /** *是否還有下一個(gè)元素 */ boolean hasNext(); /** *下一個(gè)元素 */ E next(); /** * 從集合中刪除最后一個(gè)返回的元素 */ default void remove() { throw new UnsupportedOperationException("remove"); } /** * 傳入一個(gè)Consumer對(duì)剩余的每個(gè)元素執(zhí)行指定的操作,直到所有元素已處理或操作引發(fā)異常 */ default void forEachRemaining(Consumer<? super E> action) { //requireNonNull 靜態(tài)方法將會(huì)在參數(shù)為null時(shí)主動(dòng)拋出NullPointerException 異常。 Objects.requireNonNull(action); while (hasNext()) action.accept(next()); } }
public interface ListIterator<E> extends Iterator<E> { /** * 是否有后繼 */ boolean hasNext(); /** * 后繼節(jié)點(diǎn) */ E next(); /** * 是否有前驅(qū) */ boolean hasPrevious(); /** * 前驅(qū)節(jié)點(diǎn) */ E previous(); /** * 下一個(gè)節(jié)點(diǎn)的下標(biāo) */ int nextIndex(); /** * 前驅(qū)節(jié)點(diǎn)的下標(biāo) */ int previousIndex(); /** * 移除元素 */ void remove(); /** * 替換最后返回的元素 */ void set(E e); /** * 插入一個(gè)結(jié)點(diǎn)到最后返回的元素之后 */ void add(E e); }
普通版 ArrayListdie迭代器
調(diào)用方法:list.iterator();
public Iterator<E> iterator() { return new Itr(); }
private class Itr implements Iterator<E> { int cursor; // 當(dāng)前下標(biāo) int lastRet = -1; // 最后一個(gè)元素的索引,沒有返回1 int expectedModCount = modCount;//創(chuàng)建迭代器時(shí)列表被修改的次數(shù),防止多線程操作。快速失敗 /** * 先檢查一下是否列表已經(jīng)被修改過 * 做一些簡(jiǎn)單的越界檢查 * 返回元素,并且記錄下返回元素的下標(biāo)給lastRet,當(dāng)前下標(biāo)+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]; } /** * 調(diào)用ArrayListdie自身的remove方法移除元素 * ArrayListdie自身的remove 會(huì)修改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遍歷的所有元素做一個(gè)遍歷消費(fèi) @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(); } //檢查是否列表已經(jīng)被修改了 final void checkForComodification() { if (modCount != expectedModCount) throw new ConcurrentModificationException(); } }
增強(qiáng)版本 ArrayListdie迭代器
實(shí)現(xiàn)了ListIterator接口,ListIterator接口繼承Iterator接口。并且ListItr extends Itr。Itr有的方法其都有。并且增加了
- hasPrevious 是否有前驅(qū)
- nextIndex 下一個(gè)索引位置
- previousIndex 前驅(qū)的索引位置
- previous 前驅(qū)元素
- set 替換最后返回的元素
- add 插入一個(gè)結(jié)點(diǎn)到最后返回的元素之后
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]; } //根據(jù)lastRet設(shè)置元素 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(); } } }
重點(diǎn)看一下LinkedList的迭代器
調(diào)用方法:list.iterator();
重點(diǎn)看下remove方法
private class ListItr implements ListIterator<E> { //返回的節(jié)點(diǎn) private Node<E> lastReturned; //下一個(gè)節(jié)點(diǎn) private Node<E> next; //下一個(gè)節(jié)點(diǎn)索引 private int nextIndex; //修改次數(shù) private int expectedModCount = modCount; ListItr(int index) { //根據(jù)傳進(jìn)來的數(shù)字設(shè)置next等屬性,默認(rèn)傳0 next = (index == size) ? null : node(index); nextIndex = index; } //直接調(diào)用節(jié)點(diǎn)的后繼指針 public E next() { checkForComodification(); if (!hasNext()) throw new NoSuchElementException(); lastReturned = next; next = next.next; nextIndex++; return lastReturned.item; } //返回節(jié)點(diǎn)的前驅(qū) public E previous() { checkForComodification(); if (!hasPrevious()) throw new NoSuchElementException(); lastReturned = next = (next == null) ? last : next.prev; nextIndex--; return lastReturned.item; } /** * 最重要的方法,在LinkedList中按一定規(guī)則移除大量元素時(shí)用這個(gè)方法 * 為什么會(huì)比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)的過程是
先逐一移動(dòng)指針,再找到要移除的Node,最后再修改這個(gè)Node前驅(qū)后繼等移除Node。如果有批量元素要按規(guī)則移除的話這么做時(shí)間復(fù)雜度O(n²)。但是使用迭代器是O(n)。
先看看list.remove(idnex)是怎么處理的
LinkedList是雙向鏈表,這里示意圖簡(jiǎn)單畫個(gè)單鏈表
比如要移除鏈表中偶數(shù)元素,先循環(huán)調(diào)用get方法,指針逐漸后移獲得元素,比如獲得index = 1;指針后移兩次才能獲得元素。
當(dāng)發(fā)現(xiàn)元素值為偶數(shù)是。使用idnex移除元素,如list.remove(1);鏈表先Node node(int index)返回該index下的元素,與get方法一樣。然后再做前驅(qū)后繼的修改。所以在remove之前相當(dāng)于做了兩次get請(qǐng)求。導(dǎo)致時(shí)間復(fù)雜度是O(n)。
繼續(xù)移除下一個(gè)元素需要重新再走一遍鏈表(步驟忽略當(dāng)index大于半數(shù),鏈表倒序查找)
以上如果移除偶數(shù)指針做了6次移動(dòng)。
刪除2節(jié)點(diǎn)
get請(qǐng)求移動(dòng)1次,remove(1)移動(dòng)1次。
刪除4節(jié)點(diǎn)
get請(qǐng)求移動(dòng)2次,remove(2)移動(dòng)2次。
迭代器的處理
迭代器的next指針執(zhí)行一次一直向后移動(dòng)的操作。一共只需要移動(dòng)4次。當(dāng)元素越多時(shí)這個(gè)差距會(huì)越明顯。整體上移除批量元素是O(n),而使用list.remove(index)移除批量元素是O(n²)
到此這篇關(guān)于java中LinkedList使用迭代器優(yōu)化移除批量元素原理的文章就介紹到這了,更多相關(guān)LinkedList 迭代器批量移除內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot結(jié)合前端實(shí)現(xiàn)網(wǎng)頁跳轉(zhuǎn)功能實(shí)例
今天處理Springboot統(tǒng)一異常攔截的時(shí)候,遇到了頁面跳轉(zhuǎn)的問題,這篇文章主要給大家介紹了關(guān)于springboot結(jié)合前端實(shí)現(xiàn)網(wǎng)頁跳轉(zhuǎn)功能的相關(guān)資料,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-12-12使用MyBatis攔截器實(shí)現(xiàn)SQL的完整打印
當(dāng)我們使用Mybatis結(jié)合Mybatis-plus進(jìn)行開發(fā)時(shí),為了查看執(zhí)行sql的信息通常我們可以通過屬性配置的方式打印出執(zhí)行的sql語句,但這樣的打印出了sql語句常帶有占位符信息,不利于排錯(cuò),所以本文介紹了構(gòu)建MyBatis攔截器,實(shí)現(xiàn)SQL的完整打印,需要的朋友可以參考下2024-07-07解決IDEA創(chuàng)建第一個(gè)spring boot項(xiàng)目提示cannot resolve xxx等
這篇文章主要介紹了解決IDEA創(chuàng)建第一個(gè)spring boot項(xiàng)目提示cannot resolve xxx等錯(cuò)誤問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01Java實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼的示例代碼
這篇文章主要介紹了Java實(shí)現(xiàn)滑動(dòng)驗(yàn)證碼的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01Mybatis Plus代碼生成器(時(shí)間管理大師)
這篇文章主要介紹了Mybatis Plus代碼生成器(時(shí)間管理大師)的相關(guān)知識(shí),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07java實(shí)現(xiàn)oracle插入當(dāng)前時(shí)間的方法
這篇文章主要介紹了java實(shí)現(xiàn)oracle插入當(dāng)前時(shí)間的方法,以實(shí)例形式對(duì)比分析了java使用Oracle操作時(shí)間的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-03-03基于java servlet過濾器和監(jiān)聽器(詳解)
下面小編就為大家?guī)硪黄趈ava servlet過濾器和監(jiān)聽器(詳解)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10Spring Boot 快速搭建微服務(wù)框架詳細(xì)教程
SpringBoot是為了簡(jiǎn)化Spring應(yīng)用的創(chuàng)建、運(yùn)行、調(diào)試、部署等而出現(xiàn)的,使用它可以做到專注于Spring應(yīng)用的開發(fā),而無需過多關(guān)注XML的配置。本文重點(diǎn)給大家介紹Spring Boot 快速搭建微服務(wù)框架詳細(xì)教程,需要的的朋友參考下吧2017-09-09