File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes RandomAccess Interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "RandomAccess Interface" Watch "RandomAccess Interface" New topic

RandomAccess Interface

Ruchika Kapoor

Joined: Oct 03, 2004
Posts: 22
How does RandomAccess interface helps in faster access of Vector/ArrayList. This is just a marker interface that how does implementing it improvess access time.
Ulf Dittmer

Joined: Mar 22, 2005
Posts: 42965
The key is in the code that works with objects/classes that implement this interface (this is true for all marker interfaces, by the way). You'll find these if you search through the source files of the Java class libraries looking for "instanceof RandomAccess" (which is how you check an object for whether it implements this interface).

I'll spare you the suspense and point out that it's used mostly in the java.util.Collections class. Have look at its source code; wherever you see "instanceof RandomAccess", it'll invariably be part of an "if" statement that decides which of two algorithms to use. And the one being used if the instanceof check is successful is generally (much) faster than the other one.
[ July 24, 2008: Message edited by: Ulf Dittmer ]
I agree. Here's the link:
subject: RandomAccess Interface
It's not a secret anymore!