Reputation: 342
I was studying collections in java in between I got stuck a point that some classes like ArrayList implements RandomAccess too while some classes don't. I want to know that why this interface is implemented and what its benefit? What would happen if I use this Interface in my class?
Upvotes: 9
Views: 4859
Reputation: 3894
Random Access List is a list where you can access any random data in a constant and a faster pace whereas sequential list is where you have to iterate sequentially through all the item before it, to access any particular item.
As in the below graphical image, you can see that in first example, if you want to access 9, you can directly get the values using index whereas in the second image, the data can not be accessed randomly and needs to be iterated through 23 -> 3 -> 17 -> 9
or 42 -> 9
and hence time in accessing any value in the second case is not constant and differs for each value.
Why RandomAccess interface is implemented?
Introduced as part of JDK 1.4, it is a marker interface which indicates that the class implementing it takes constant and fast time in accessing any random data in list.
What is the benefit?
Any implementation of the algorithm can check if the current list is a child of Random Access and accordingly ensure the best performance for random access or sequential access.
Below is one great example from one blog which explains one of the usage in a great way:
Object o;
if (listObject instanceof RandomAccess)
{
for (int i=0, n=list.size(); i < n; i++)
{
o = list.get(i); // directly get the object as list implements Random access
//do something with object o
}
}
else
{
Iterator itr = list.iterator();
for (int i=0, n=list.size(); i < n; i++)
{
o = itr.next(); // Use iterator to get values sequentially as random access
// is not fast for this list and hence does not implement RandomAccess
//do something with object o
}
}
What would happen if I use this Interface in my class?
Same will happen as happens when you implement any other marker interface. It just gives an indication to other class as what can be expected from the class implementing it.
You can also refer Java docs to understand more about it.
Upvotes: 7
Reputation: 3807
RandomAccess
is a marker interface. It doesn't have any methods in it.
Its marks ArrayList
to show that it supports random access of any value in O(1)
given index.
LinkedList
, on the other hand, doesn't provide O(1)
random access to its elements because its backed by a doubly linked list (hence O(n)
random access complexity). Hence it doesn't make sense to use RandomAccess
for LinkedList
. Same reasoning can be given to other Collection
classes that doesn't implement RandomAccess
.
I want to know that why this interface is implemented and what its benefit?
Java collection framework uses this interface to optimize performance.
Straight out of docs:
As a rule of thumb, a List implementation should implement this interface if, for typical instances of the class, this loop:
for (int i=0, n=list.size(); i < n; i++) list.get(i);
runs faster than this loop:
for (Iterator i=list.iterator(); i.hasNext(); ) i.next();
Also see this javadoc here which explains it beautifully.
Upvotes: 3