java 中RandomAccess接口源碼分析
java 中RandomAccess接口源碼分析
RandomAccess是一個接口,位于java.util包中。
這個接口的作用注釋寫的很清楚了:
/**
* 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ù)時間)隨機(jī)訪問。
* 這個接口的主要目的是允許一般的算法更改它們的行為,以便在隨機(jī)或者順序存取列表時能提供更好的性能。
* <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)用于順序存取列表時,有可能產(chǎn)生二次項行為。
* 泛型算法列表鼓勵在將某個算法應(yīng)用于順序存取列表可能導(dǎo)致差的性能之前,先檢查給定的列表是否是這個接口的一個實(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)在變得很大時會導(dǎo)致漸進(jìn)的非線性訪問時間,但實(shí)際上是常量訪問時間。
* 這樣的List實(shí)現(xiàn)通常都應(yīng)該實(shí)現(xiàn)該接口。
* 一般來說,某個List實(shí)現(xiàn)如果(對某些典型的類的實(shí)例來說)滿足下面的條件,就應(yīng)該實(shí)現(xiàn)這個接口:循環(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>.
* 這個接口是Java集合框架的一員。
* @since 1.4
*/
public interface RandomAccess {
}
RandomAccess是一個空接口,而空接口的作用一般是起到一個標(biāo)識的作用。
通俗點(diǎn)講,就是判斷一個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)文章
SpringAOP中的動態(tài)代理技術(shù)深入解析
這篇文章主要介紹了SpringAOP中的動態(tài)代理技術(shù)深入解析,spring默認(rèn)使用JDK動態(tài)代理實(shí)現(xiàn)AOP,類如果實(shí)現(xiàn)了接口,spring就會用JDK動態(tài)代理實(shí)現(xiàn)AOP,如果目標(biāo)類沒有實(shí)現(xiàn)接口,spring則使用Cglib動態(tài)代理來實(shí)現(xiàn)AOP,需要的朋友可以參考下2024-01-01
Java關(guān)于后端怎么去接收Date、LocalDateTime類型的參數(shù)詳解
這篇文章主要介紹了java關(guān)于后端怎么去接收Date、LocalDateTime類型的參數(shù),文中有詳細(xì)的代碼流程,對我們學(xué)習(xí)或工作有一定的參考價值,需要的朋友可以參考下2023-06-06
MyBatis-Plus 查詢返回實(shí)體對象還是map
這篇文章主要介紹了MyBatis-Plus 查詢返回實(shí)體對象還是map,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
javaweb設(shè)計中filter粗粒度權(quán)限控制代碼示例
這篇文章主要介紹了javaweb設(shè)計中filter粗粒度權(quán)限控制代碼示例,小編覺得還是挺不錯的,需要的朋友可以參考。2017-10-10

