Stop casting to List (Java)(paultyma.blogspot.com) |
Stop casting to List (Java)(paultyma.blogspot.com) |
Java actually provides a marker interface to distinguish between implementations of lists that support fast random access [1] and those that don't, and library code is encouraged to use this to decide an optimal codepath.
[1] https://docs.oracle.com/javase/8/docs/api/java/util/RandomAc...
Collections.emptyList() Collections.singletonList() Collections.unmodifiableList() List.of()
As niftich points out, use the marker interface java.util.RandomAccess, if you want to know if the list allows constant time access.