• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do we achieve fast access when we implement RandomAccess interface  RSS feed

 
Ashwin Ganesh
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do we achieve fast access when we implement RandomAccess interface?
Knowing that, Random Access interface is a marker interface(which does not have any abstract methods defined in it)

So just by implementing a marker interface, how are we achieving faster access on an ArrayList or Vector?

Please through me some light on this topic. Thanks in advance.
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've got it the other way around. That's like saying I can put a Post-It note on my Toyota Corolla that says "This is a Porsche" and my Corolla becomes a Porsche.  It doesn't work like that. You don't achieve faster access just by implementing the RandomAccess marker interface. 

Rather, and this is explained in the API documentation for java.util.RandomAccess, if your implementation is such that for typical instances of the implementation class, you'd get better performance by using get(i) than you would by using an Iterator and its next() method, then you should mark your class as implementing RandomAccess.

Extending the analogy I gave earlier, if you actually do have a Porsche, then it should have a Porsche emblem somewhere on it so people who don't know much about cars can at least see the emblem and say, "Oh, that's a Porsche."
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!