RandomAccess is the token interface in Java used by List implementations to indicate that they have quick, random access to their elements. Since it is specifically designed for List implementations, why not in the List hierarchy? For example, consider the following method, which requires entering RandomAccess as input and returns a random element:
public <E, L extends List<E> & RandomAccess> E getRandomElement(L list) {...}
I need to pass this method either an anonymous list, or a list with the declared type ArrayList<E> , a specific class, and not a list declared using some interface, for example List<E> . However, if RandomAccess was parameterized and extended with List<E> , then my method might look like this:
public <E, L extends RandomAccess<E>> E getRandomElement(L list) {...}
and I could pass it a list with the declared type RandomAccess<E> , which is an interface, not a concrete class. ArrayList<E> etc. then it will implement RandomAccess<E> , not List<E> .
source share