Java List Interface

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

  1. public interface List<E> extends Collection<E>

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

  1. import java.util.*;
  2. public class ListExample{
  3. public static void main(String args[]){
  4. ArrayList<String> al=new ArrayList<String>();
  5. al.add(“Amit”);
  6. al.add(“Vijay”);
  7. al.add(“Kumar”);
  8. al.add(1,“Sachin”);
  9. System.out.println(“Element at 2nd position: “+al.get(2));
  10. for(String s:al){
  11.  System.out.println(s);
  12. }
  13. }
  14. }

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

  1. public interface ListIterator<E> extends Iterator<E>

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

  1. import java.util.*;
  2. public class TestCollection8{
  3. public static void main(String args[]){
  4. ArrayList<String> al=new ArrayList<String>();
  5. al.add(“Amit”);
  6. al.add(“Vijay”);
  7. al.add(“Kumar”);
  8. al.add(1,“Sachin”);
  9. System.out.println(“element at 2nd position: “+al.get(2));
  10. ListIterator<String> itr=al.listIterator();
  11. System.out.println(“traversing elements in forward direction…”);
  12. while(itr.hasNext()){
  13. System.out.println(itr.next());
  14. }
  15. System.out.println(“traversing elements in backward direction…”);
  16. while(itr.hasPrevious()){
  17. System.out.println(itr.previous());
  18. }
  19. }
  20. }

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

  1. import java.util.*;
  2. class Book {
  3. int id;
  4. String name,author,publisher;
  5. int quantity;
  6. public Book(int id, String name, String author, String publisher, int quantity) {
  7.     this.id = id;
  8.     this.name = name;
  9.     this.author = author;
  10.     this.publisher = publisher;
  11.     this.quantity = quantity;
  12. }
  13. }
  14. public class ListExample {
  15. public static void main(String[] args) {
  16.     //Creating list of Books
  17.     List<Book> list=new ArrayList<Book>();
  18.     //Creating Books
  19.     Book b1=new Book(101,“Let us C”,“Yashwant Kanetkar”,“BPB”,8);
  20.     Book b2=new Book(102,“Data Communications & Networking”,“Forouzan”,“Mc Graw Hill”,4);
  21.     Book b3=new Book(103,“Operating System”,“Galvin”,“Wiley”,6);
  22.     //Adding Books to list
  23.     list.add(b1);
  24.     list.add(b2);
  25.     list.add(b3);
  26.     //Traversing list
  27.     for(Book b:list){
  28.     System.out.println(b.id+” “+b.name+” “+b.author+” “+b.publisher+” “+b.quantity);
  29.     }
  30. }
  31. }

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
java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

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 public interface List<E> extends Collection<E> 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 […]

Tags:

....
...