-1

How Implementation of

Random Access Marker Interface

is making retrieval operation in ArrayList faster? Marker Interface requests JVM for faster access or retrieval of values.

What algorithm is JVM implementing for faster access?

Hope You will understand the question now.

2 Answers2

1

It doesn't.

It tells us that the implementation supports fast random access.

That's the purpose of a marker interface. It marks an implementation.

To quote the docs

Marker interface used by List implementations to indicate that they support fast (generally constant time) random access. The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists.

Thihara
  • 7,031
  • 2
  • 29
  • 56
1

It marks the implementations of list which can be accessed randomly.

This way a caller can either access the data randomly if that is efficient, or use another means if it is not.

e.g. In AbstractList you have an implementation which can be optimised for RandomAccess or not.

public List<E> subList(int fromIndex, int toIndex) {
    return (this instanceof RandomAccess ?
            new RandomAccessSubList<>(this, fromIndex, toIndex) :
            new SubList<>(this, fromIndex, toIndex));
}

What algorithm is JVM implementing for faster access?

The important method is get(int) which needs to be O(1)

public E get(int index) {
    rangeCheck(index);

    return elementData(index);
}

calls

E elementData(int index) {
    return (E) elementData[index];
}

as you can see it just looks up the index in an array so it O(1)

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130