In this article, we will discuss List interface with all its important methods in detail
1. Key point about List:
- allows duplicate items
- maintains insertion order
2. List interface:
- List is a sub-interface of Collection interface (i.e.; List extends Collection)
- To represent a group of element/objects as a single unit/entity, which allows to add duplicate element/objects as well as maintains insertion-order, then programmer should consider using List
- So, List allows duplicates element/objects maintaining insertion-order
- There are 3 concrete classes for List interface viz.; ArrayList, LinkedList and Vector
- List interface defines various list specific methods; in addition to inherited Collection methods
- These specific methods can be used to operate on list objects only
- The biggest advantage of using List specific methods is, we can insert/access element/objects of List by their position using zero-based index
- Present in java.util package and extends java.util.Collection interface
Source: Team BenchResources.Net
3. List interface methods:
List methods | Description |
void add(int index, Object obj); | add/inserts a single element/object to invoking collection at the specified index |
boolean addAll(int index, Collection c); | add/inserts group of element/objects to invoking collection at the specified index
(i.e.; adding specified collection to invoking collection at the specified index) |
Object get(int index); | retrieve element/object from the invoking collection at the specified index |
Object remove(int index); | remove/deletes a single element/object from invoking collection at the specified index |
Object set(int index, Object newObj); | replaces old object from invoking collection with new object passed at the specified index
returns old object; |
List subList(int start, int end) | returns a view of the portion of this list between the specified fromIndex (inclusive) and toIndex (exclusive)
Note: If fromIndex and toIndex are equal, the returned list is empty |
int indexOf(Object obj); | returns first occurrence of specified element/object in the invoking collection
returns -1, if list doesn’t contain the element |
int lastIndexOf(Object obj); | returns last occurrence of specified element/object in the invoking collection
returns -1, if list doesn’t contain the element |
ListIterator listIterator; | returns listIterator for the invoking collection; to iterate over the collection elements in both FORWARD & BACKWARD directions |
Note : All above listed List methods will be inherited to subsequent List implemented classes
4. Below listed classes implements List interface
- ArrayList
- LinkedList
- Vector
5. Factors to consider while discussing any collection class
We should consider below factors while discussing any implementation class of collection framework or for that matter Map interface,
- Underlying data structure
- Duplicates are allowed or Not
- Insertion order is maintained or Not
- Whether NULL insertion is possible or Not
- If possible, how many NULL values can be inserted
- Whether collection class provide sorting, by default
- Is there any way to apply customized sorting
- Performance, while dealing with retrieval or manipulation (addition/deletion)
- By default, all methods are synchronized or Not
Related Articles:
- List interface
- ArrayList class
- LinkedList class
- Vector class
- Stack class
- ArrayList v/s Vector
- ArrayList v/s LinkedList
- List v/s Set
References:
- https://docs.oracle.com/javase/tutorial/collections/intro/
- https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html
- https://docs.oracle.com/javase/7/docs/api/java/util/Collection.html
- https://docs.oracle.com/javase/tutorial/collections/interfaces/list.html
- https://docs.oracle.com/javase/7/docs/api/java/util/List.html
Happy Coding !!
Happy Learning !!