激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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

服務器之家 - 編程語言 - Java教程 - Java List的remove()方法陷阱以及性能優化

Java List的remove()方法陷阱以及性能優化

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

Java List在進行remove()方法是通常容易踩坑,本文就詳細的介紹一下陷阱以及性能優化,感興趣的可以了解一下

Java List在進行remove()方法是通常容易踩坑,主要有一下幾點

循環時:問題在于,刪除某個元素后,因為刪除元素后,后面的元素都往前移動了一位,而你的索引+1,所以實際訪問的元素相對于刪除的元素中間間隔了一位。

 

幾種常見方法

1.使用for循環不進行額外處理時(錯誤

//錯誤的方法
for(int i=0;i<list.size();i++) {
	if(list.get(i)%2==0) {
		list.remove(i);
	}
}

2.使用foreach循環(錯誤

for(Integer i:list) {
  if(i%2==0) {
   	list.remove(i);
  }
}

拋出異常:java.util.ConcurrentModificationException;
foreach的本質是使用迭代器實現,每次進入for (Integer i:list) 時,會調用ListItr.next()方法;
繼而調用checkForComodification()方法, checkForComodification()方法對操作集合的次數進行了判斷,如果當前對集合的操作次數與生成迭代器時不同,拋出異常

public E next() {
	checkForComodification();
	if (!hasNext()) {
		 throw new NoSuchElementException();
	}
	 lastReturned = next;
	next = next.next;
	nextIndex++;
	return lastReturned.item;
}
// checkForComodification()方法對集合遍歷前被修改的次數與現在被修改的次數做出對比
final void checkForComodification() {
	  if (modCount != expectedModCount) {
	  		 throw new ConcurrentModificationException();
	  }
           
}

使用for循環,并且同時改變索引;(正確

//正確
for(int i=0;i<list.size();i++) {
	if(list.get(i)%2==0) {
		list.remove(i);
		i--;//在元素被移除掉后,進行索引后移
	}
}

使用for循環,倒序進行;(正確

//正確
for(int i=list.size()-1;i>=0;i--) {
	if(list.get(i)%2==0) {
		list.remove(i);
	}
}

使用while循環,刪除了元素,索引便不+1,在沒刪除元素時索引+1(正確

//正確
int i=0;
while(i<list.size()) {
	if(list.get(i)%2==0) {
		list.remove(i);
	}else {
		i++;
	}
}

4.使用迭代器方法(正確,推薦

只能使用迭代器的remove()方法,使用列表的remove()方法是錯誤的

//正確,并且推薦的方法
Iterator<Integer> itr = list.iterator();
while(itr.hasNext()) {
	if(itr.next()%2 ==0)
		itr.remove();
}

 

性能分析

下面來談談當數據量過大時候,需要刪除的元素較多時,如何用迭代器進行性能的優化,對于ArrayList這幾乎是致命的,從一個ArrayList中刪除批量元素都是昂貴的時間復雜度為O(n玻,那么接下來看看LinkeedList是否可行。LinkedList暴露了兩個問題,一個:是每次的Get請求效率不高,而且,對于remove的調用同樣低效,因為達到位置I的代價是昂貴的。

是每次的Get請求效率不高
需要先get元素,然后過濾元素。比較元素是否滿足刪除條件。

remove的調用同樣低效
LinkedList的remove(index),方法是需要先遍歷鏈表,先找到該index下的節點,再處理節點的前驅后繼。

以上兩個問題當遇到批量級別需要處理時時間復雜度直接上升到O(n玻

 

使用迭代器的方法刪除元素

對于LinkedList,對該迭代器的remove()方法的調用只花費常數時間,因為在循環時該迭代器位于需要被刪除的節點,因此是常數操作。對于一個ArrayList,即使該迭代器位于需要被刪除的節點,其remove()方法依然是昂貴的,因為數組項必須移動。下面貼出示例代碼以及運行結果

Java List的remove()方法陷阱以及性能優化

public class RemoveByIterator {

	public static void main(String[] args) {
		
		List<Integer> arrList1 = new ArrayList<>();
		for(int i=0;i<100000;i++) {
			arrList1.add(i);
		}
		
		List<Integer> linList1 = new LinkedList<>();
		for(int i=0;i<100000;i++) {
			linList1.add(i);
		}

		List<Integer> arrList2 = new ArrayList<>();
		for(int i=0;i<100000;i++) {
			arrList2.add(i);
		}
		
		List<Integer> linList2 = new LinkedList<>();
		for(int i=0;i<100000;i++) {
			linList2.add(i);
		}
		
		removeEvens(arrList1,"ArrayList");
		removeEvens(linList1,"LinkedList");
		removeEvensByIterator(arrList2,"ArrayList");
		removeEvensByIterator(linList2,"LinkedList");
		
	}
	public static void removeEvensByIterator(List<Integer> lst ,String name) {//利用迭代器remove偶數
		long sTime = new Date().getTime();
		Iterator<Integer> itr = lst.iterator();
		while(itr.hasNext()) {
			
			if(itr.next()%2 ==0)
				itr.remove();
		}
		
		System.out.println(name+"使用迭代器時間:"+(new Date().getTime()-sTime)+"毫秒");
	}
	
	public static void removeEvens(List<Integer> list , String name) {//不使用迭代器remove偶數
		long sTime = new Date().getTime();
		int i=0;
		while(i<list.size()) {
			
			if(list.get(i)%2==0) {
				list.remove(i);
			}else {
				i++;
			}
		}
	
		System.out.println(name+"不使用迭代器的時間"+(new Date().getTime()-sTime)+"毫秒");
	}
}

原理 重點看一下LinkedList的迭代器

另一篇博客Iterator簡介 LinkedList使用迭代器優化移除批量元素原理
調用方法:list.iterator();

Java List的remove()方法陷阱以及性能優化

重點看下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 List的remove()方法陷阱以及性能優化

Java List的remove()方法陷阱以及性能優化

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

Java List的remove()方法陷阱以及性能優化

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

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

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

 

迭代器的處理

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

Java List的remove()方法陷阱以及性能優化

到此這篇關于Java List的remove()方法陷阱以及性能優化的文章就介紹到這了,更多相關Java List的remove() 內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日日操日日操 | 2021年无线乱码播放高清完整 | 欧美在线成人影院 | 国产在线观看一区二区三区 | 久久精品视频首页 | 少妇的肉体2无删减版 | 国产在线播放91 | 一级黄色免费大片 | 国产福利不卡一区二区三区 | 国内毛片视频 | 亚洲第一男人天堂 | 有色视频在线观看 | 中文字幕一二三区芒果 | 午夜影院操 | 国产精品久久久久久久久久久久久久久 | 日韩精品中文字幕一区 | 国产精品热 | 久久久www成人免费精品 | 男女一边摸一边做羞羞视频免费 | 国产一级二级在线播放 | 精品国产一区二区三区天美传媒 | 91视频官网 | 久久6国产 | 在线观看福利网站 | 凹凸成人精品亚洲精品密奴 | 国产亚洲网 | 天天操天天碰 | 国产精品久久久久久久av三级 | 日本精品久久久一区二区三区 | 免费中文视频 | 黄色大片在线免费观看 | 91成人免费视频 | av成人免费在线观看 | 国产一级毛片视频在线! | 色婷婷综合久久久中文一区二区 | 中文亚洲视频 | 成人在线不卡 | 亚洲欧美日韩精品久久 | 极品美女一级毛片 | 国产精品成人av片免费看最爱 | 午夜a狂野欧美一区二区 |