java 中RandomAccess接口源碼分析
java 中RandomAccess接口源碼分析
RandomAccess是一個(gè)接口,位于java.util包中。
這個(gè)接口的作用注釋寫的很清楚了:
/**
* Marker interface used by <tt>List</tt> implementations to indicate that
* they support fast (generally constant time) random access. The primary
* purpose of this interface is to allow generic algorithms to alter their
* behavior to provide good performance when applied to either random or
* sequential access lists.
* List實(shí)現(xiàn)所使用的標(biāo)記接口,用來表明實(shí)現(xiàn)了這些接口的list支持快速(通常是常數(shù)時(shí)間)隨機(jī)訪問。
* 這個(gè)接口的主要目的是允許一般的算法更改它們的行為,以便在隨機(jī)或者順序存取列表時(shí)能提供更好的性能。
* <p>The best algorithms for manipulating random access lists (such as
* <tt>ArrayList</tt>) can produce quadratic behavior when applied to
* sequential access lists (such as <tt>LinkedList</tt>). Generic list
* algorithms are encouraged to check whether the given list is an
* <tt>instanceof</tt> this interface before applying an algorithm that would
* provide poor performance if it were applied to a sequential access list,
* and to alter their behavior if necessary to guarantee acceptable
* performance.
* 操作隨機(jī)訪問列表(如ArrayList)的最佳算法在應(yīng)用于順序存取列表時(shí),有可能產(chǎn)生二次項(xiàng)行為。
* 泛型算法列表鼓勵(lì)在將某個(gè)算法應(yīng)用于順序存取列表可能導(dǎo)致差的性能之前,先檢查給定的列表是否是這個(gè)接口的一個(gè)實(shí)例,
* 并在需要時(shí)去改變這些算法的行為以保證性能。
* <p>It is recognized that the distinction between random and sequential
* access is often fuzzy. For example, some <tt>List</tt> implementations
* provide asymptotically linear access times if they get huge, but constant
* access times in practice. Such a <tt>List</tt> implementation
* should generally implement this interface. As a rule of thumb, a
* <tt>List</tt> implementation should implement this interface if,
* for typical instances of the class, this loop:
* 隨機(jī)訪問和順序存取之間的界限通常是模糊的。例如,一些List實(shí)現(xiàn)在變得很大時(shí)會(huì)導(dǎo)致漸進(jìn)的非線性訪問時(shí)間,但實(shí)際上是常量訪問時(shí)間。
* 這樣的List實(shí)現(xiàn)通常都應(yīng)該實(shí)現(xiàn)該接口。
* 一般來說,某個(gè)List實(shí)現(xiàn)如果(對某些典型的類的實(shí)例來說)滿足下面的條件,就應(yīng)該實(shí)現(xiàn)這個(gè)接口:循環(huán)
* <pre>
* for (int i=0, n=list.size(); i < n; i++)
* list.get(i);
* </pre>
* runs faster than this loop:
* 比下面的循環(huán)運(yùn)行速度快。
* <pre>
* for (Iterator i=list.iterator(); i.hasNext(); )
* i.next();
* </pre>
*
* <p>This interface is a member of the
* <a href="{@docRoot}/../technotes/guides/collections/index.html" rel="external nofollow" >
* Java Collections Framework</a>.
* 這個(gè)接口是Java集合框架的一員。
* @since 1.4
*/
public interface RandomAccess {
}
RandomAccess是一個(gè)空接口,而空接口的作用一般是起到一個(gè)標(biāo)識的作用。
通俗點(diǎn)講,就是判斷一個(gè)list是否實(shí)現(xiàn)了RandomAcess接口,如果實(shí)現(xiàn)了,采用下面所示的簡單的for循環(huán)進(jìn)行訪問速度比較快:
for (int i=0, n=list.size(); i < n; i++) list.get(i);
如果未實(shí)現(xiàn)RandomAcess接口,則采用下面的iterator循環(huán)訪問速度比較快。
for (Iterator i=list.iterator(); i.hasNext(); ) i.next();
判斷使用instanceof,即
if (list instanceof RandomAccess)
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
基于Java實(shí)現(xiàn)PDF文本旋轉(zhuǎn)傾斜
這篇文章主要介紹了基于Java實(shí)現(xiàn)PDF文本旋轉(zhuǎn)傾斜,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-05-05
MybatisPlus創(chuàng)建時(shí)間不想用默認(rèn)值的問題
MybatisPlus通過FieldFill注解和MpMetaObjectHandler類支持自動(dòng)填充字段功能,特別地,可以設(shè)置字段在插入或更新時(shí)自動(dòng)填充創(chuàng)建時(shí)間和更新時(shí)間,但在特定場景下,如導(dǎo)入數(shù)據(jù)時(shí),可能需要自定義創(chuàng)建時(shí)間2024-09-09
Java微信公眾平臺開發(fā)(12) 微信用戶信息的獲取
這篇文章主要為大家詳細(xì)介紹了Java微信公眾平臺開發(fā)第十二步,微信用戶信息的獲取,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-04-04
基于springcloud異步線程池、高并發(fā)請求feign的解決方案
這篇文章主要介紹了基于springcloud異步線程池、高并發(fā)請求feign的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02
SpringBoot中的yaml語法及靜態(tài)資源訪問問題
這篇文章主要介紹了SpringBoot中的yaml語法及靜態(tài)資源訪問問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-07-07
java+selenium 網(wǎng)易云音樂刷累計(jì)聽歌數(shù)的方法
這篇文章主要介紹了java+selenium 網(wǎng)易云音樂刷累計(jì)聽歌數(shù)的方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06

