日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java ArrayList 为什么要实现 RandomAccess 接口?

發(fā)布時間:2025/3/21 java 23 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java ArrayList 为什么要实现 RandomAccess 接口? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在我們的開發(fā)中,List接口是最常見不過,而且我們幾乎每天都在用ArrayList或者LinkedList,但是細心的同學有沒有發(fā)現,ArrayList中實現了RandomAccess接口,而LinkedList卻沒有實現RandomAccess接口,這是為什么呢?

public?class?ArrayList<E>?extends?AbstractList<E>implements?List<E>,?RandomAccess,?Cloneable,?java.io.Serializable public?class?LinkedList<E>extends?AbstractSequentialList<E>implements?List<E>,?Deque<E>,?Cloneable,?java.io.Serializable

RandomAccess接口中是空的,RandomAccess接口又是什么呢?

public?interface?RandomAccess?{ }

RandomAccess接口

RandomAccess是一個標記接口,官方解釋是只要List實現這個接口,就能支持快速隨機訪問。而什么是隨機訪問呢?接下來我們來舉例說明。

Collections是集合的一個工具類,我們看一下Collections源碼中的二分搜索方法。

public?static?<T>int?binarySearch(List<??extends?Comparable<??super?T>>?list,?T?key)?{if?(list?instanceof?RandomAccess?||?list.size()<BINARYSEARCH_THRESHOLD)return?Collections.indexedBinarySearch(list,?key);elsereturn?Collections.iteratorBinarySearch(list,?key);}

在源碼中可以看出,判斷l(xiāng)ist是否是RandomAccess的實例,如果是,則執(zhí)行indexedBinarySearch方法,如果不是,則執(zhí)行iteratorBinarySearch方法。接下來看一下這兩個方法。

private?static?<T> int?indexedBinarySearch(List<??extends?Comparable<??super?T>>?list,?T?key)?{int?low?=?0;int?high?=?list.size()-1;while?(low?<=?high)?{int?mid?=?(low?+?high)?>>>?1;Comparable<??super?T>?midVal?=?list.get(mid);int?cmp?=?midVal.compareTo(key);if?(cmp?<?0)low?=?mid?+?1;else?if?(cmp?>?0)high?=?mid?-?1;elsereturn?mid;?//?key?found}return?-(low?+?1);??//?key?not?found } private?static?<T> int?iteratorBinarySearch(List<??extends?Comparable<??super?T>>?list,?T?key) {int?low?=?0;int?high?=?list.size()-1;ListIterator<??extends?Comparable<??super?T>>?i?=?list.listIterator();while?(low?<=?high)?{int?mid?=?(low?+?high)?>>>?1;Comparable<??super?T>?midVal?=?get(i,?mid);int?cmp?=?midVal.compareTo(key);if?(cmp?<?0)low?=?mid?+?1;else?if?(cmp?>?0)high?=?mid?-?1;elsereturn?mid;?//?key?found}return?-(low?+?1);??//?key?not?found }

上述兩個方法的源碼表示,實現了RandomAccess接口的List使用索引遍歷,而未實現RandomAccess接口的List使用迭代器遍歷。那么為什么要這么設計呢?
既然涉及到二分搜索的遍歷操作,那么現在我們來分析一下ArrayList和LinkedList遍歷元素的性能如何?

public?class?CollectionTest?{public?static?void?main(String[]?args){long?arrayListIndexedTime?=?arrayListIndexed();long?arrayListIteratorTime?=?arrayListIterator();long?linkedListIndexedTime?=?linkedListIndexed();long?linkedListIteratorTime?=?linkedListIterator();System.out.println("測試ArrayList通過for遍歷所消耗時間:"?+?arrayListIndexedTime);System.out.println("測試ArrayList通過iterator遍歷所消耗時間:"?+?arrayListIteratorTime);System.out.println("測試LinkedList通過for遍歷所消耗時間:"?+?linkedListIndexedTime);System.out.println("測試LinkedList通過iterator遍歷所消耗時間:"?+?linkedListIteratorTime);}//測試ArrayList通過for遍歷所消耗時間public?static?long?arrayListIndexed()?{List<Integer>?arrayList?=?new?ArrayList<>();for?(int?i?=?0;?i?<?10000;?i++)?{arrayList.add(i);}//記錄開始時間long?startTime?=?System.currentTimeMillis();for?(int?i?=?0;?i?<?arrayList.size();?i++)?{arrayList.get(i);}//記錄結束時間long?endTime?=?System.currentTimeMillis();//遍歷消耗時間long?resultTime?=?endTime?-?startTime;return?resultTime;}//測試ArrayList通過iterator遍歷所消耗時間public?static?long?arrayListIterator()?{List<Integer>?arrayList?=?new?ArrayList<>();for?(int?i?=?0;?i?<?10000;?i++)?{arrayList.add(i);}//記錄開始時間long?startTime?=?System.currentTimeMillis();Iterator<Integer>?iterator?=?arrayList.iterator();while?(iterator.hasNext())?{iterator.next();}//記錄結束時間long?endTime?=?System.currentTimeMillis();//遍歷消耗時間long?resultTime?=?endTime?-?startTime;return?resultTime;}//測試LinkedList通過for遍歷所消耗時間public?static?long?linkedListIndexed()?{List<Integer>?linkedList?=?new?LinkedList<>();for?(int?i?=?0;?i?<?10000;?i++)?{linkedList.add(i);}//記錄開始時間long?startTime?=?System.currentTimeMillis();for?(int?i?=?0;?i?<?linkedList.size();?i++)?{linkedList.get(i);}//記錄結束時間long?endTime?=?System.currentTimeMillis();//遍歷消耗時間long?resultTime?=?endTime?-?startTime;return?resultTime;}//測試LinkedList通過iterator遍歷所消耗時間public?static?long?linkedListIterator()?{List<Integer>?linkedList?=?new?LinkedList<>();for?(int?i?=?0;?i?<?10000;?i++)?{linkedList.add(i);}//記錄開始時間long?startTime?=?System.currentTimeMillis();Iterator<Integer>?iterator?=?linkedList.iterator();while?(iterator.hasNext())?{iterator.next();}//記錄結束時間long?endTime?=?System.currentTimeMillis();//遍歷消耗時間long?resultTime?=?endTime?-?startTime;return?resultTime;} }

測試結果如下

測試ArrayList通過for遍歷所消耗時間:1 測試ArrayList通過iterator遍歷所消耗時間:2 測試LinkedList通過for遍歷所消耗時間:47 測試LinkedList通過iterator遍歷所消耗時間:1

我們來分析一下測試結果:ArrayList通過for遍歷比通過iterator遍歷要稍快,LinkedList通過iterator遍歷比通過for遍歷要快。

所以說在我們的應用中,要考慮使用List接口的哪種實現類,可以更好更高效的滿足實際場景需求。所以在這里通過實現RandomAccess接口來區(qū)分List的哪種實現類。

總結

最后總結一句話:實現RandomAccess接口的List可以通過for循環(huán)來遍歷數據比使用iterator遍歷數據更高效,未實現RandomAccess接口的List可以通過iterator遍歷數據比使用for循環(huán)來遍歷數據更高效。

總結

以上是生活随笔為你收集整理的Java ArrayList 为什么要实现 RandomAccess 接口?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。