Marker interfaces do not define behavior directly, instead they "mark" a class as having a particular capability. In the case of
Serializable
the interface specifies that if the class is serialized using the serialization I/O classes, then a NotSerializableException
will not be thrown (unless the object contains some other class that cannot be serialized). Cloneable
similarly indicates that the use of the Object.clone()
method for a Cloneable
class will not throw a CloneNotSupportedException
.
The
RandomAccess
interface identifies that a particular java.util.List
implementation has fast random access. More accurately, the RandomAccess
interface identifies List implementations which are faster to iterate using the List.get()
method rather than using the Iterator.next()
method.
However, when you want to iterate
Many of the utility methods in Collections class use a variant of an algorithm depending on whether or not the object is a List
classes, then you can optimize the iteration speed by using RandomAccess
RandomAccess
object. public static int binarySearch(List list, Object key) {
if (list instanceof RandomAccess || list.size() < BINARYSEARCH_THRESHOLD)
return indexedBinarySearch(list, key);
else
return iteratorBinarySearch(list, key);
}
Read full article from Learning From Code: Fast Random Access, page 1 of 2public static void fill(List list, Object obj) { int size = list.size();
if (size < FILL_THRESHOLD || list instanceof RandomAccess) { for (int i=0; i< size; i++) list.set(i, obj); } else { ListIterator itr = list.listIterator(); for (int i=0; i< size; i++) { itr.next(); itr.set(obj); } } }