• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do we achieve fast access when we implement RandomAccess interface

 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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."
 
reply
    Bookmark Topic Watch Topic
  • New Topic