Introduction to RandomAccess Interface

Posted By : Harish Kalra | 31-Aug-2019

 

RandomAccess Interface : 

 

SDK 1.4 presented a java.util.RandomAccess interface for optimizing List execution, yet it has no methods.

 

What Does RandomAccess Mean?

 

RandomAccess is a marker interface, like the Serializable and Cloneable interfaces. All these marker interfaces do not define methods. Rather, they distinguish a class as having a specific capacity. For the situation of Serializable, the interface specifies that if the class is serialized using the serialization I/O classes, a NotSerializableException will not be thrown (except if the object contains some other class that cannot be serialized). Cloneable similarly indicates that the utilization of the Object.clone( ) method for a Cloneable class will not throw a CloneNotSupportedException.

The RandomAccess interface recognizes that a specific java.util.List implementation has fast random access. (A progressively precise name for the interface would have been "FastRandomAccess.") This interface attempts to characterize a loose idea: what precisely is fast? The documentation gives a straightforward guide: whenever repeated access utilizing the List.get( ) method is faster than repeated access utilizing the Iterator.next( ) method, at that point the List has fast random access. The two kinds of access have appeared in the following code examples

 

Repeated access using List.get( ):

Object obj
for (int j=0, num=list.size(  ); j < num; j++)
  obj = list.get(j);

Repeated access using Iterator.next( ):

Object obj;
for (Iterator iter=list.iterator(  ); iter.hasNext(  ); )
  obj = iter.next(  );

A third loop joins the past two loops to stay away from the repeated Iterator.hasNext( ) test on each loop iteration:

Object obj;
Iterator iter=list.iterator(  );
for (int j=0, n=list.size(  ); j < n; j++)
  obj = iter.next(  );
        

 

How Is RandomAccess Used?

 

So since we know what RandomAccess means, how would we use it? There are two aspects to utilizing the other marker interfaces, Serializable and Cloneable: characterizing classes that implement them and utilizing their abilities by means of ObjectInput/ObjectOutput and Object.clone( ), individually. RandomAccess is somewhat extraordinary. Obviously, despite everything we have to choose whether a specific class implements it, yet the potential classes are seriously confined: RandomAccess ought to be implemented uniquely in java.util.List classes. Also, most such classes are made outside of projects. The SDK gives the most every now and again utilized implementations, and subclasses of the SDK classes don't have to implement RandomAccess on the grounds that they consequently inherit the ability where fitting.

The subsequent aspect, utilizing the RandomAccess capacity, is additionally unique. Regardless of whether a class is Serializable or Cloneable is naturally distinguished when you use ObjectInput/ObjectOutput and Object.clone( ). In any case, RandomAccess has no such programmed help. Rather, you have to expressly check whether a class implements RandomAccess utilizing the instanceof operator: 

if (listObject instanceof RandomAccess)
  ...

You should then expressly pick the fitting access method, List.get( ) or Iterator.next( ). Plainly, on the off chance that we test for RandomAccess on each loop iteration, we would make a ton of redundant calls and most likely losing the advantage of RandomAccess also. So the pattern to follow in utilizing RandomAccess makes the test outside the loop. The canonical pattern resembles this:

Object obj;
if (listObject instanceof RandomAccess)
{
    for (int j=0, num=list.size( ); j < num; j++) {
      obj = list.get(j); 
      //do something with object obj 
    }
 } 
else 
{
    Iterator iter = list.iterator( );
    for (int j=0, num=list.size( ); j < num; j++)
    {
      obj = iter.next( );
      //do something with object obj 
    }
}

 

Conclusion: To RandomAccess Interface,  Retrieval Operation is fast in ArrayList.

 

Thanks 

 

 

 

About Author

Author Image
Harish Kalra

He is active in his work, as well as a quick learner. He has been a good resource to our company.

Request for Proposal

Name is required

Comment is required

Sending message..