List Interface

java.util.List
public interface List extends Collection
The List interface extends Collection interface that maintain their elements in order and can contain duplicates. The elements in a list are ordered. Elements can be inserted or accessed by their position in the list, using a zero-based index.
List interface features:
  • It can allow insert duplicate elements.
  • It allows multiple null elements.
  • It is used to access elements based on their numerical position in the list.
  • It is used to search for a specific element in the list and returns its numerical position.
Method of List Interface
  1. void add(int index, Object element)
              Inserts the specified element at the specified position in this list (optional operation).
  2. boolean add(Object o)
              Appends the specified element to the end of this list (optional operation).
  3. boolean addAll(Collection c)
              Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation).
  4. boolean addAll(int index, Collection c)
              Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
  5. void clear()
              Removes all of the elements from this list (optional operation).
  6. boolean contains(Object o)
              Returns true if this list contains the specified element.
  7. boolean containsAll(Collection c)
              Returns true if this list contains all of the elements of the specified collection.
  8. boolean equals(Object o)
              Compares the specified object with this list for equality.

  9. Object get(int index)
              Returns the element at the specified position in this list.
  10. int hashCode()
              Returns the hash code value for this list.
  11. int indexOf(Object o)
              Returns the index in this list of the first occurrence of the specified element, or -1 if this list does not contain this element.
  12. boolean isEmpty()
              Returns true if this list contains no elements.
  13. Iterator iterator()
              Returns an iterator over the elements in this list in proper sequence.
  14. int lastIndexOf(Object o)
              Returns the index in this list of the last occurrence of the specified element, or -1 if this list does not contain this element.
  15. ListIterator listIterator()
              Returns a list iterator of the elements in this list (in proper sequence).
  16. ListIterator listIterator (int index)
              Returns a list iterator of the elements in this list (in proper sequence), starting at the specified position in this list.
  17. Object remove(int index)
              Removes the element at the specified position in this list (optional operation).
  18. boolean remove(Object o)
              Removes the first occurrence in this list of the specified element (optional operation).
  19. boolean removeAll(Collection c)
              Removes from this list all the elements that are contained in the specified collection (optional operation).
  20. boolean retainAll(Collection c)
              Retains only the elements in this list that are contained in the specified collection (optional operation).
  21. Object set(int index, Object element)
              Replaces the element at the specified position in this list with the specified element (optional operation).
  22. int size()
              Returns the number of elements in this list.
  23. List subList(int fromIndex, int toIndex)
              Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
  24. Object[] toArray()
              Returns an array containing all of the elements in this list in proper sequence.
  25. Object[] toArray(Object[] a)
              Returns an array containing all of the elements in this list in proper sequence; the runtime type of the returned array is that of the specified array.
Example of List interface
import java.util.List; import java.util.ArrayList; import java.util.Iterator; public class ListDemo { public static void main(String ar[]) { // Create a list List list= new ArrayList(); //Adding elements to the List list.add("Core java"); list.add("Struts"); list.add("Spring"); list.add("j2ee"); list.add("EJB"); list.add("Hibernate"); // Find the size of list System.out.println("There are "+list.size()+" element in the list"); System.out.println("contents of list are......."); // Display list element using iterator method Iterator itr= list.iterator(); while(itr.hasNext()) { System.out.println(itr.next()); } System.out.println("Removing EJB from the list....."); list.remove("EJB"); System.out.println("No. of elements after removal :"+ list.size()); System.out.println("Searching struts in the list ........"); System.out.println("Searching element is :" + list.contains("Struts")); } }
Output

There are 6 element in the list
contents of list are.......
Core java
Struts
Spring
j2ee
EJB
Hibernate
Removing EJB from the list.....
No. of elements after removal :5
Searching struts in the list ........
Searching element is :true


Download this example
 



 

Latest Tutorial


Most Visited Tutorial