Javatpoint Logo
Javatpoint Logo

Java List Interface

List Interface is the subinterface of Collection.It contains methods to insert and delete elements in index basis.It is a factory of ListIterator interface.

List Interface declaration

Methods of Java List Interface

Method Description
void add(int index,Object element) It is used to insert element into the invoking list at the index passed in the index.
boolean addAll(int index,Collection c) It is used to insert all elements of c into the invoking list at the index passed in the index.
object get(int index) It is used to return the object stored at the specified index within the invoking collection.
object set(int index,Object element) It is used to assign element to the location specified by index within the invoking list.
object remove(int index) It is used to remove the element at position index from the invoking list and return the deleted element.
ListIterator listIterator() It is used to return an iterator to the start of the invoking list.
ListIterator listIterator(int index) It is used to return an iterator to the invoking list that begins at the specified index.

Java List Example

Output:

Element at 2nd position: Vijay
Amit
Sachin
Vijay
Kumar

Java ListIterator Interface

ListIterator Interface is used to traverse the element in backward and forward direction.

ListIterator Interface declaration

Methods of Java ListIterator Interface:

Method Description
boolean hasNext() This method return true if the list iterator has more elements when traversing the list in the forward direction.
Object next() This method return the next element in the list and advances the cursor position.
boolean hasPrevious() This method return true if this list iterator has more elements when traversing the list in the reverse direction.
Object previous() This method return the previous element in the list and moves the cursor position backwards.

Example of ListIterator Interface

Test it Now

Output:

element at 2nd position: Vijay
traversing elements in forward direction...
Amit
Sachin
Vijay
Kumar
traversing elements in backward direction...
Kumar
Vijay
Sachin
Amit

Example of ListIterator Interface: Book

Output:

101 Let us C Yashwant Kanetkar BPB 8
102 Data Communications & Networking Forouzan Mc Graw Hill 4
103 Operating System Galvin Wiley 6
Next TopicJava HashSet class




Please Share

facebook twitter google plus pinterest tumbler

Learn Latest Tutorials