List for ordered collections.Set for colections without duplicates.Queue for ordered collections with constraints on addition and deletion.An ordered collection can be accessed in two ways:
hasNext() and next())a[i] in Python)For Random Access, additional functions are present.
public interface List<E> extends Collection<E>{
void add(int index, E element);
void remove(int index):
E get(int index); //retuns an element at index.
E set(int index, E element); //Sets an element E at index.
//Look at add() function. This seems similar....
//Difference? -> set give an object of type E as return value. What is this value?
ListIterator<E> listIterator(); //Special Type of Iterator for lists.
ListIterator extends Iterator. Special functions are:
void add(E element): To insert an element before the current index.void previous(): To go to the previous element.boolean hasPrevious(): Checks that it is legal to go backwards.List , random access may not be that efficient.
ii, we have to from beginning and traverse i links.get to enable random access.<aside> 💡 Solution: Set a tagging interface called Random Access.
</aside>
List supports random access or not.if(c instanceof RandomAccess){
//use random access algorithm
}
else{
//use sequential access algorithm
}
Now, only the implementations that have this RandomAccess Tagging interface implemented, will use in the above, the random access algorithm.
If a List is not implemeting this tagging interface, then it will use the sequential access algorithm. → So can reduce the computations.