List interface:
A List represents an ordered or sequenced group of elements. It may contain duplicate elements. It extends the collection interface.
 Note:
- Elements can be inserted or retrieved by their position in the list. Position or index value starts from 0.
- List interface defines its own methods in addition to the methods of collection interface.
List Interface declaration:
public interface List extends Collection |
 Commonly used methods of List interface:
1. add(int index, Object obj): Insert obj at the specified index position into this list. All pre-existing elements at or beyond the specified index are shifted up.
Syntax:
public void add(int index, Object obj)
2. addAll(int index, Collection c): Insert all elements of the specified collection at the specified index position into this collection. All pre-existing elements at or beyond the specified index are shifted up. Returns true if this list changes after the operation otherwise return false.
Syntax:
public boolean addAll(int index, Collection c)
3. get(int index): Returns the object stored at specified index position into this list.
Syntax:
public Object get(int index)
4. indexOf(Object obj): Returns index of first occurrence of obj in this list. It returns -1 if obj is not exist in this list.
Syntax:
public int indexOf(Object obj)
5. lastIndexOf(Object obj): Returns index of last occurrence of obj in this list. It returns -1 if obj is not exist in this list.
Syntax:
public int lastIndexOf(Object obj)
6. listIterator( ): Returns an ListIterator object to the start of the this list.
Syntax:
public ListIterator listIterator( )
7. listIterator(int index): Returns an ListIterator object to this list that begins at the specified index.
Syntax:
public ListIterator listIterator(int index)
8. remove(int index): Removes the element at specified index position from this list and returns the deleted element. Indexes of all elements beyond index are decremented by one.
Syntax: public Object remove(int index)
9. set(int index, Object obj): Assigns obj to the specified index location within the this list.
Syntax:
public Object set(int index, Object obj)
10. subList(int start, int end): Returns a list that contains the elements from start to end-1 index position in this list.
Syntax:
public List subList(int start, int end)
 A simple example of ArrayList class to explain few methods of List interface.
 ArrayListTest.java
import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * This class is used to show the ArrayList functionality. * @author w3spoint */ public class ArrayListTest { public static void main(String args[]){ //Create ArrayList object. List arrayList = new ArrayList(); //Add objects to the List. arrayList.add("Amani"); arrayList.add("Prabhjot"); arrayList.add("Nidhi"); arrayList.add("Vandana"); arrayList.add("Poonam"); //Size of the ArrayList object. System.out.println("Size: " + arrayList.size()); //Print the ArrayList object. System.out.println("ArrayList elements:"); System.out.println(arrayList); //Print the ArrayList elements using iterator. Iterator iterator1=arrayList.iterator(); System.out.println("ArrayList elements " + "using iterator:"); while(iterator1.hasNext()){ System.out.println(iterator1.next()); } //Add an object at a specific position. arrayList.add(2,"Jagdeep"); //Remove an element from a specific position. arrayList.remove(3); arrayList.remove(4); //Size of the ArrayList object. System.out.println("Size after manipulation: " + arrayList.size()); //Print the ArrayList object. System.out.println("ArrayList elements" + " after manipulation:"); System.out.println(arrayList); //Print the ArrayList elements using iterator. Iterator iterator2=arrayList.iterator(); System.out.println("ArrayList elements after" + " manipulation using iterator:"); while(iterator2.hasNext()){ System.out.println(iterator2.next()); } } } |
Output:
Size: 5 ArrayList elements: [Amani, Prabhjot, Nidhi, Vandana, Poonam] ArrayList elements using iterator: Amani Prabhjot Nidhi Vandana Poonam Size after manipulation: 4 ArrayList elements after manipulation: [Amani, Prabhjot, Jagdeep, Vandana] ArrayList elements after manipulation using iterator: Amani Prabhjot Jagdeep Vandana |
Next Topic: Map interface in java with example.
Previous Topic: SortedSet interface in java with example.