java 中RandomAccess接口源碼分析
java 中RandomAccess接口源碼分析
RandomAccess是一個(gè)接口,位于java.util包中。
這個(gè)接口的作用注釋寫(xiě)的很清楚了:
/** * 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)記接口,用來(lái)表明實(shí)現(xiàn)了這些接口的list支持快速(通常是常數(shù)時(shí)間)隨機(jī)訪問(wèn)。 * 這個(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ī)訪問(wèn)列表(如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ī)訪問(wèn)和順序存取之間的界限通常是模糊的。例如,一些List實(shí)現(xiàn)在變得很大時(shí)會(huì)導(dǎo)致漸進(jìn)的非線性訪問(wèn)時(shí)間,但實(shí)際上是常量訪問(wèn)時(shí)間。 * 這樣的List實(shí)現(xiàn)通常都應(yīng)該實(shí)現(xiàn)該接口。 * 一般來(lái)說(shuō),某個(gè)List實(shí)現(xiàn)如果(對(duì)某些典型的類的實(shí)例來(lái)說(shuō))滿足下面的條件,就應(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)識(shí)的作用。
通俗點(diǎn)講,就是判斷一個(gè)list是否實(shí)現(xiàn)了RandomAcess接口,如果實(shí)現(xiàn)了,采用下面所示的簡(jiǎn)單的for循環(huán)進(jìn)行訪問(wèn)速度比較快:
for (int i=0, n=list.size(); i < n; i++) list.get(i);
如果未實(shí)現(xiàn)RandomAcess接口,則采用下面的iterator循環(huán)訪問(wèn)速度比較快。
for (Iterator i=list.iterator(); i.hasNext(); ) i.next();
判斷使用instanceof,即
if (list instanceof RandomAccess)
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
相關(guān)文章
SpringAOP中的動(dòng)態(tài)代理技術(shù)深入解析
這篇文章主要介紹了SpringAOP中的動(dòng)態(tài)代理技術(shù)深入解析,spring默認(rèn)使用JDK動(dòng)態(tài)代理實(shí)現(xiàn)AOP,類如果實(shí)現(xiàn)了接口,spring就會(huì)用JDK動(dòng)態(tài)代理實(shí)現(xiàn)AOP,如果目標(biāo)類沒(méi)有實(shí)現(xiàn)接口,spring則使用Cglib動(dòng)態(tài)代理來(lái)實(shí)現(xiàn)AOP,需要的朋友可以參考下2024-01-01Java關(guān)于后端怎么去接收Date、LocalDateTime類型的參數(shù)詳解
這篇文章主要介紹了java關(guān)于后端怎么去接收Date、LocalDateTime類型的參數(shù),文中有詳細(xì)的代碼流程,對(duì)我們學(xué)習(xí)或工作有一定的參考價(jià)值,需要的朋友可以參考下2023-06-06MyBatis-Plus 查詢返回實(shí)體對(duì)象還是map
這篇文章主要介紹了MyBatis-Plus 查詢返回實(shí)體對(duì)象還是map,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09javaweb設(shè)計(jì)中filter粗粒度權(quán)限控制代碼示例
這篇文章主要介紹了javaweb設(shè)計(jì)中filter粗粒度權(quán)限控制代碼示例,小編覺(jué)得還是挺不錯(cuò)的,需要的朋友可以參考。2017-10-10