Java List Interface

Java List Interface
  • List Interface is the of Collection. 
  • The Java.util.List is a child interface/subinterface of Collection. 
  • It is a factory of ListIterator interface.
  • It contains index-based methods to insert and delete 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 Interface is implemented by the classes of ArrayList, LinkedList, Vector and Stack.
Syntax:
public interface List extends Collection
  • How to create List Objects:
    • Objects can be non-generic and generic.
Generic Vs Non-Generic object:
  • Java collection framework was non-generic before JDK 1.5, since 1.5, it is generic.
  • Java's new generic collection allows you to have only one type of object in the collection. So, it is type-safe for typecasting dows not required at run time.
  • Example of old non-generic java collection.
  • Example:
  •               List AL = new ArrayList();               List LL = new LinkedList();               List V = new Vector();               List S = new Stack();
  • Example of new non-generic java collection.
  • Example:
  •               List <string> al = new ArrayList<string>();
  • In generic collection, we need to specify the type in angular braces.
  • After the introduction of Generics in Java 1.5, it is possible to restrict the type of object that can be stored in the List. 
  • Example: List&ltObj&gt list = new ArrayList&ltObj&gt ();
  • Note: Obj is the type of object to be stored in List
Methods:
  • How to add a given element at a specified index?
    • void add(int index, Object O): This method adds a given element at a specified index.
    • Example: 
                         List l1 = new ArrayList();
                                          l1.add(0, 1);
                                         l1.add(1, 2);
  • How to print all elements?
    • Example:  System.out.println(l1);
  • How to add collection in to list?
    • boolean addAll(int index, Collection c): This method adds all elements from specified collection to list. 
    • Example:
                  List l2 = new ArrayList();
                                                 l2.add(1);
                                                 l2.add(2);
                                                 l2.add(3);
                  l1.addAll(1, l2);

  • How to remove a specific item at position?
    • Object remove(int index): This method removes an element from the specified index.
    • Example: l1.remove(1);
  • How to get specific item at position?
    • Object get(int index): This method returns an element at the specified index.
    • Example:  System.out.println(l1.get(3)); 
  • How to change specific item at the position?
    • Object set(int index, Object new): This method replaces the element at a given index with a new element. 
    • Example: l1.set(0, 5);
  • How to search the first occurrence of the given element?
    • int indexOf(Object o): This method returns the first occurrence of a given element or -1 if an element is not present in the list.
    • Example: System.out.println("first index of 2:"+ l1.indexOf(2)); 
  • How to search the last occurrence of the given element?
    • int lastIndexOf(Object o): This method returns the last occurrence of a given element or -1 if an element is not present in the list.
    • Example: System.out.println("last index of 2:"+ l1.lastIndexOf(2));
  • How to get a portion of the given List between two indices?
    • List subList(int fromIndex, int toIndex):This method returns List view of specified List between fromIndex(inclusive) and toIndex(exclusive).
    • Example:
List range = new ArrayList();
                          // Return List from 2nd(including) and 4th element(excluding)
                          range = l1.subList(2, 4);
System.out.println("Sublist : "+ range);
  • How to clear all elements from list?
    • void clear(): It is used to remove all of the elements from this list.
    • Example:
l1.clear();
  • How to display all elements using for each loop?
    • Example:
for(Integer s:l1){
                                             System.out.println(s);
}
  • Write all method of Java ListIterator Interface?
    • void add(E e): This method inserts the specified element into the list.
    • boolean hasNext(): This method returns true if the list iterator has more elements while traversing the list in the forward direction.
    • E next(): This method returns the next element in the list and advances the cursor position.
    • int nextIndex(): This method returns the index of the element that would be returned by a subsequent call to next()
    • boolean hasPrevious(): This method returns true if this list iterator has more elements while traversing the list in the reverse direction.
    • E previous(): This method returns the previous element in the list and moves the cursor position backward.
    • E previousIndex(): This method returns the index of the element that would be returned by a subsequent call to previous().
    • void remove(): This method removes the last element from the list that was returned by next() or previous() methods
    • void set(E e): This method replaces the last element returned by next() or previous() methods with the specified element.
  • How to traversing elements in forward direction using ListIterator?
    • Example:
ListIterator itr=l1.listIterator(); 
                       System.out.println("Traversing elements in forward direction"); 
                       while(itr.hasNext()){ 
System.out.println("index:"+itr.nextIndex()+" value:"+itr.next());
                       } 
  • How to traversing elements in backward direction using ListIterator?
    • Example:
ListIterator itr=l1.listIterator(); 
System.out.println("Traversing elements in backward direction"); 
                      while(itr.hasPrevious()){   
                      System.out.println("index:"+itr.previousIndex()+"value:"+itr.previous());
                      }

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form