一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java中LinkedList使用迭代器優化移除批量元素原理

java中LinkedList使用迭代器優化移除批量元素原理

2022-03-07 13:07wsdfym Java教程

本文主要介紹了java中LinkedList使用迭代器優化移除批量元素原理,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文主要介紹了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);
}

 

普通版 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;//創建迭代器時列表被修改的次數,防止多線程操作。快速失敗
      /**
      * 先檢查一下是否列表已經被修改過
      * 做一些簡單的越界檢查
      * 返回元素,并且記錄下返回元素的下標給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迭代器

java中LinkedList使用迭代器優化移除批量元素原理

實現了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();

java中LinkedList使用迭代器優化移除批量元素原理

重點看下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)。

 

先看看list.remove(idnex)是怎么處理的

LinkedList是雙向鏈表,這里示意圖簡單畫個單鏈表
比如要移除鏈表中偶數元素,先循環調用get方法,指針逐漸后移獲得元素,比如獲得index = 1;指針后移兩次才能獲得元素。
當發現元素值為偶數是。使用idnex移除元素,如list.remove(1);鏈表先Node node(int index)返回該index下的元素,與get方法一樣。然后再做前驅后繼的修改。所以在remove之前相當于做了兩次get請求。導致時間復雜度是O(n)。

java中LinkedList使用迭代器優化移除批量元素原理

java中LinkedList使用迭代器優化移除批量元素原理

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

java中LinkedList使用迭代器優化移除批量元素原理

以上如果移除偶數指針做了6次移動。

刪除2節點
get請求移動1次,remove(1)移動1次。

刪除4節點
get請求移動2次,remove(2)移動2次。

 

迭代器的處理

迭代器的next指針執行一次一直向后移動的操作。一共只需要移動4次。當元素越多時這個差距會越明顯。整體上移除批量元素是O(n),而使用list.remove(index)移除批量元素是O(n玻

java中LinkedList使用迭代器優化移除批量元素原理

到此這篇關于java中LinkedList使用迭代器優化移除批量元素原理的文章就介紹到這了,更多相關LinkedList 迭代器批量移除內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/wsdfym/article/details/109633368

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日韩在线二区全免费 | 色综合亚洲精品激情狠狠 | 久久www免费人成_看片高清 | 日韩不卡一区二区三区 | 免费日本在线视频 | 亚洲美色综合天天久久综合精品 | 亚洲精品午夜视频 | 亚洲高清在线视频 | 国产自一区 | 美女脱了内裤打开腿让男人图片 | 公交车强校花系列小说 | 国产视频一二三区 | 亚洲免费视频播放 | 国产一区二区三区高清 | 丝袜护士强制脚足取精 | 欧美又大又粗又爽视频 | 国内亚州视频在线观看 | 欧美午夜视频一区二区 | 国产一级特黄aa大片免费 | 成人免费观看网欧美片 | 国产老村长足疗店对白 | 日本不卡在线视频高清免费 | 日本三级做a全过程在线观看 | 国产欧美精品 | 精品免费视在线视频观看 | 狠狠色婷婷狠狠狠亚洲综合 | 亚洲色图15p| 免费av在线看| 青青青久热国产精品视频 | 色先锋影音先锋 | 美女脱了内裤打开腿让人桶网站o | 蜜桃视频一区二区 | 成人在线视频在线观看 | 欧美2区| 精品国产福利在线观看一区 | 亚洲国产精品综合欧美 | 国产精品nv在线观看 | 亚洲国内精品 | 五月色天在线视频综合观看 | 高清一区 | 电车痴汉中文字幕 |