Scrivio Interview library
ConceptIntermediate6 min read · updated 12 November 2026

What is the RandomAccess marker interface, and how do JDK algorithms such as Collections.binarySearch use it?

RandomAccess is a marker interface with no methods: a list implements it to say that get(i) is fast, roughly constant time, as ArrayList does and LinkedList does not. JDK algorithms such as Collections.binarySearch, shuffle and reverse check instanceof RandomAccess and use index-based loops when it is present, and iterator-based loops when it is not, so they stay efficient on both kinds of list. It is only a hint, and library code with index loops can use the same check.

Why this question is asked

Generic code that works on any List faces a problem: the best algorithm depends on whether indexing is cheap, but the List interface offers get(i) to every implementation, fast or not. RandomAccess is Java's small, old solution to that problem. Interviewers ask about it because it shows whether we understand how the JDK keeps its algorithms efficient across implementations, and whether we would apply the same care in our own utilities. In this answer, we will recap what a marker interface is, see which lists implement RandomAccess, follow how binarySearch and shuffle use it, and then apply it in our own code.

1A Quick Recap of Marker Interfaces

A marker interface is an interface with no methods. Implementing it does not add behaviour; it attaches a fact to the type that other code can test with instanceof. Serializable and Cloneable are older examples. RandomAccess, added in Java 1.4, records one fact: this list supports fast random access, so get(i) costs about the same for any index.

Java
System.out.println(new ArrayList<>() instanceof RandomAccess); // true
System.out.println(new LinkedList<>() instanceof RandomAccess); // false
System.out.println(Arrays.asList(1, 2) instanceof RandomAccess); // true
System.out.println(List.of(1, 2) instanceof RandomAccess); // true
System.out.println(new CopyOnWriteArrayList<>() instanceof RandomAccess); // true

Notice that every list backed by an array implements it, and the linked one does not. There is no method to call and nothing to override; the interface is pure information. The JDK documentation gives a rule of thumb for implementers: a list should implement RandomAccess if a plain index loop over it is faster than an iterator loop.

2How Collections.binarySearch Uses It

Binary search repeatedly looks at the middle element of a sorted range and halves the range. On an array that is O(log n) gets. On a linked list each get is a walk, so an index-based binary search would do log n walks of up to n/2 nodes, which is worse than a simple linear scan.

So Collections.binarySearch checks the list before it starts. If the list implements RandomAccess, or it is small (below an internal threshold of a few thousand elements), it runs the indexed version. Otherwise it runs an iterator-based version that moves a ListIterator forwards and backwards to each midpoint, so the total movement is O(n) link hops with only O(log n) comparisons.

Java
List<Integer> sortedArray = new ArrayList<>(List.of(1, 3, 5, 7, 9, 11));
List<Integer> sortedLinked = new LinkedList<>(sortedArray);
System.out.println(Collections.binarySearch(sortedArray, 7)); // 3, indexed: ~log2(6) gets
System.out.println(Collections.binarySearch(sortedLinked, 7)); // 3, same answer, iterator-based for large lists
System.out.println(Collections.binarySearch(sortedArray, 4)); // -3: not found, would be inserted at index 2
Step throughHow Collections.binarySearch picks its algorithm
  1. Called with a sorted Listand the key to find
  2. RandomAccess, or a small list?instanceof RandomAccess or size below threshold
    YesIndexed search with get(mid)NoIterator search with a ListIterator
  3. Return index or -(insertion point) - 1same contract either way

3 steps in order. Play them, or step through them one at a time.

The caller writes the same line for both lists and gets the right algorithm for each. That is the whole point of the marker: generic code adapts without knowing the concrete class.

3Other JDK Algorithms That Check It

The same pattern appears across the Collections class. shuffle swaps elements by index when the list is RandomAccess or very small; otherwise it copies the elements into an array, shuffles the array, and writes them back through a ListIterator, because random swaps on a linked list would walk constantly. reverse, fill, copy and rotate make similar choices, using index loops on random-access lists and iterators on the others.

Wrappers keep the information too. Collections.unmodifiableList and Collections.synchronizedList return a random-access wrapper class when the list they wrap is RandomAccess, and subList on an ArrayList returns a view that is also RandomAccess. If a wrapper hid the marker, every algorithm would fall back to the slower path for no reason.

JDK methods that branch on RandomAccess
MethodRandomAccess pathOther path
Collections.binarySearchget(mid) loopListIterator moved to each midpoint
Collections.shuffleswap by indexcopy to array, shuffle, write back
Collections.reverseswap i and n-1-itwo ListIterators from both ends
Collections.fill / copyset(i) loopListIterator set()
unmodifiableList, synchronizedListrandom-access wrapperplain wrapper
Exact thresholds are internal details and can change between versions.

4Using the Check in Our Own Library Code

When we write a utility that walks a list by index, we can follow the JDK's lead: use the index loop when the list is RandomAccess and fall back to an iterator otherwise.

Java
static <T> int countMatches(List<T> list, Predicate<? super T> p) {
int count = 0;
if (list instanceof RandomAccess) {
for (int i = 0, n = list.size(); i < n; i++) if (p.test(list.get(i))) count++;
} else {
for (T t : list) if (p.test(t)) count++;
}
return count;
}
System.out.println(countMatches(new LinkedList<>(List.of(1, 2, 3, 4)), x -> x % 2 == 0)); // 2

But there is a catch: the marker is only a hint. Nothing checks that a class implementing it really has fast get(i), and a custom list could implement it wrongly or forget it. It also says nothing about thread safety or mutability. In practice, for simple traversal a for-each loop is efficient on every list, so we reach for the RandomAccess branch only in algorithms that genuinely need indexes, such as binary search, swapping or comparing distant elements.

5Follow-up questions

Should a custom List implementation implement RandomAccess?

Yes, if its get(i) is constant time or close to it, for example a list backed by an array or by a fixed-size block structure. Then JDK algorithms will use their fastest paths on it. If get(i) walks or searches, it must not implement it, or those algorithms will become slow.

Why is it a marker interface and not a method like isRandomAccess()?

Adding a method to the List interface in Java 1.4 would have broken every existing List implementation, since default methods did not exist until Java 8. A marker interface added the information without changing List at all. Today a method or a characteristic flag might be designed instead, much as Spliterator has characteristics such as SIZED.

Does a subList of an ArrayList implement RandomAccess?

Yes. AbstractList creates a random-access sublist when the backing list is RandomAccess, and ArrayList's own subList view implements it too. So algorithms on a sublist of an ArrayList still use the indexed paths.

6Conclusion

In this answer, we saw that RandomAccess is a method-less marker interface saying that get(i) is fast, implemented by ArrayList, Arrays.asList, List.of and CopyOnWriteArrayList but not LinkedList. Collections.binarySearch, shuffle, reverse and others test instanceof RandomAccess to choose index-based or iterator-based algorithms, and wrappers keep the marker. Library code can use the same check, remembering that it is only a hint.

What to say in the interviewAbout 90 seconds aloud

Open with

RandomAccess is a marker interface, so it has no methods. A List implements it to say that get of i is fast, roughly constant time for any index.

Then, if they want more

ArrayList, Arrays.asList, List.of and CopyOnWriteArrayList implement it; LinkedList does not. Its purpose is to let generic algorithms pick the right strategy without knowing the concrete class. Collections.binarySearch is the standard example: if the list is RandomAccess, or small, it does the normal indexed search with get of mid.

Otherwise it uses an iterator-based version that moves a ListIterator to each midpoint, because log n calls to get on a linked list would each walk up to half the list. shuffle does something similar: indexed swaps for random-access lists, and for others it copies to an array, shuffles that, and writes back through a list iterator. reverse, fill and copy also branch, and wrappers like unmodifiableList return random-access variants so the information is not lost.

In my own utilities I use the same check when the algorithm really needs indexes, with a for-each fallback. I would add that it is only a hint, nothing enforces it, and for plain traversal a for-each loop is already efficient on every list, so I do not sprinkle instanceof checks everywhere.

It was a marker because adding a method to List in Java 1.4 would have broken every implementation.

About this answer

Last updated
12 November 2026
Versions it names
Java 8
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "What is the RandomAccess marker interface, and how do JDK algorithms such as...": https://scrivio-interview-library.netlify.app/java/lists/7722f11ddf/