List Interface extends the collection and well if it extends then it declares its behavior by default.
A list is an ordered collection/sequence, which may have duplicate elements as well. Elements can be accessed, inserted and removed from list using index position of elements. Apart from the methods declared by Collection, list defines some of its own methods. We’ll see them going further.
Here lies the trick, if the Collection is unmodifiable then methods defined by list will throw an exception UnsupportedOperationException and ClassCastException is generated if any object is not compatible with another, as in case when we add an incompatible object to an existing collection.
Now is the time to see the methods declared by List explicitly, you can check the methods declared by Collection here.
|void add(int index, Object obj)||In the argument, index gives the index position at which obj has to be inserted. Any pre-existing value will be shifted forward and no object is overwritten.|
|boolean addAll(int index, Collection c)||The elements of Collection c is added to the list, which is invoking this method at the index position index. All the values will be shifted forward and no object is overwritten. It returns true if operation is successful and false otherwise.|
|Object get(int index)||It returns the element stored at index position index from the list invoking this method.|
|int indexOf(Object obj)||It returns the index position of first instance of object obj stored from the list invoking this method.|
|int lastIndexOf(Object obj)||It returns the index position of last instance of object obj stored from the list invoking this method.|
|ListIterator listIterator( )||It returns an iterator to enumerate the elements of list, starting zero index position.|
|ListIterator listIterator(int)||It returns an iterator to enumerate the elements of list, starting index index position.|
|Object remove(int index)||It will remove the element stored at index position index and subsequently the list size is reduced, thus index of subsequent element is reduced by one.
If the index is greater than the last index of element in list, it'll throw IndexOutOfBoundsException.
|Object set(int index, Object obj)||It will overwrite the object/element present at index position index from list invoking this method.|
|List subList(int start, int end)||Returns a list that includes elements from start to end–1 in the invoking list. Elements in the returned list are also referenced by the invoking object.|