List is a fundamental and widely-used collection type in the Java Collections Framework. The List interface extends Collection and declares the behavior of a collection that stores a sequence of elements. Elements can be inserted or accessed by their position in the list, using a zero-based index. A list may contain duplicate elements. List is a generic interface that has this declaration:
interface List<E>
Here, E specifies the type of objects that the list will hold.
In addition to the methods defined by Collection, List defines some of its own method.
Method | Description |
---|---|
void add(int index, E obj) | Inserts obj into the invoking list at the index passed in index. Any preexisting elements at or beyond the point of insertion are shifted up. Thus, no elements are overwritten. |
boolean addAll(int index, Collection<? extends E> c) | Inserts all elements of c into the invoking list at the index passed in index. Any preexisting elements at or beyond the point of insertion are shifted up. Thus, no elements are overwritten. Returns true if the invoking list changes and returns false otherwise. |
E get(int index) | Returns the object stored at the specified index within the invoking collection. |
int indexOf(Object obj) | Returns the index of the first instance of obj in the invoking list. If obj is not an element of the list, –1 is returned. |
int lastIndexOf(Object obj) | Returns the index of the last instance of obj in the invoking list. If obj is not an element of the list, –1 is returned. |
ListIterator
|
Returns an iterator to the start of the invoking list. |
ListIterator
|
Returns an iterator to the invoking list that begins at the specified index. |
E remove(int index) | Removes the element at position index from the invoking list and returns the deleted element. The resulting list is compacted. That is, the indexes of subsequent elements are decremented by one. |
default void replaceAll(UnaryOperator<E> opToApply) | Updates each element in the list with the value obtained from the opToApply function. (Added by JDK 8.) |
E set(int index, E obj) | Assigns obj to the location specified by index within the invoking list. Returns the old value. |
default void sort(Comparator<? super E> comp) | Sorts the list using the comparator specified by comp. (Added by JDK 8.) |
List
|
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. |
Most Commonly thrown Exceptions in List
Exception | Description |
---|---|
UnsupportedOperationException | will throw if the list can't be modified. |
ClassCastException | occurs when an attempt is made to add an incompatible object to a list. |
IndexOutOfBoundsException | will throw if an invalid index is used. |
NullPointerException | will throw when you try to store a null object and null elements are not allowed in the list. |
IllegalArgumentException | will throw when an invalid argument is used. |
factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.
ReplyDeletefactorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.
factorial hundred In the last few days, the “factorial of 100” is one of the top subjects and a lot of maths geeks compute it using voice assistants such as Alexa, Shiri, etc.