Java Tutorial

List

java.util.List:

  1. List is the sub-interface of java.util.Collection interface.
  2. List object also allows us to add duplicates.
  3. List object automatically displays the data in sorted order.
  4. List object allows us to add an element either at the ending position or at specific position.
  5. List object allows us to retrieve the data in forward direction, backward direction and random retrieval.

List: List is an interface which extends Collection.

Methods inList interface:

  1. public Object get (int): This method is used for obtaining that element from the specified position. If the get method is not returning any value because of invalid position the value of object of object is NULL.

  2. public Object remove (int): This method is used for removing the objects from List object based on position.

  3. public Object remove (Object): This method is used for removing the objects from List object based on content.

  4. Public void add (int pos, Object): This method is used for adding an object at the specified position.

  5. public void addAll (int pos, Collection): This method is used for adding one Collection object to another Collection object at the specified position.

  6. public List headList (Object obj): This method is used for obtaining those objects Xi's which are less than or equal to target object (Xi≤obj).

  7. public List tailList (Object obj): This method is used for obtaining those objects Xi's which are greater than target object (Xi>obj or Xi≥(obj-1)).

  8. public List subList (Object obj1, Object obj2): This method is used for obtaining those values which are a part of List i.e., range of values [or] In subList method is select those values Xi's which are less than or equal to object 1 and greater than object 2 (obj1≤Xi<obj2).

  9. public ListIterator listIterator (): This method is used for extracting the data from List object either in forward or in backward or in both the directions. ListIterator is an interface which extends Iterator interface. This interface contains the following methods:

public boolean hasPrevious (); - 1
public Object previous (); - 2
public void set (Object); - 3

All the methods of iterator are also coming. Method-1 is used for checking weather we have previous element or not, this method returns true as long as we have previous elements otherwise false. Method-2 is used for obtaining previous element. Method-3 is used for modifying the existing Collection object returns true. Methods 2 and 3 can be used as long as method-1 returns true.