The List Interface
 Method: void add(int index, Object o)
 Inserts specified element at specified position
Example:
list.add(4, a); //adds element a at position 4
The List Interface
 Method: boolean add(Object o)
 Adds specified element to the end of the list
Example:
list.add(a); //adds element a at the end of the list
The List Interface
 Method: boolean contains(Object 0)
 Returns true if the specified element is in the list
Example:
list.add(a);
list.contains(a); //returns true
////////////////////////////////////////////////////////////
list.add(b);
list.contains(a); //returns false
The List Interface
 Method: Object get(int index)
 Returns the element at the specified position
Example:
list.add(a);
list.add(b);
list.get(1); //returns b
The List Interface
 Method: int indexOf(object o);
 Returns the index of the specified element or else -1
Example:
list.add(a);
list.add(b);
list.add(c);
list.indexOf(c); //returns 2
The List Interface
 Method: int lastIndexOf(object o);
 Returns the last index of the specified element or
else -1
Example:
list.add(a);
list.add(b);
list.add(a);
list.indexOf(a); //returns 2
The List Interface
 Method: Object remove(int index)
 Removes the element at the specified index
Example:
list.add(a);
list.add(b);
list.add(c);
list.remove(1); //removes element a
The List Interface
 Method: boolean remove(Object o)
 Removes the first occurrence of the specified element
Example:
list.add(a);
list.add(b);
list.add(a);
list.remove(a); //removes element a at position 0
The List Interface
 Method: object set(int index, Object element)
 Replaces the element at the specified position and
returns the old element
Example:
list.add(a);
list.set(0, b); //replaces element a with b and
returns element a
The List Interface
 Method: boolean isEmpty()
 Returns true if the method is empty
Example
list.isEmpty(); //retruns true
////////////////////////////////////////////////////////////
list.add(a);
list.isEmpty(); //returns false
The List Interface
 Method: int size()
 Returns the number of elements in the list
Example:
list.add(a);
list.add(b);
list.add(c);
list.size(); //Returns 3
The List Interface
 Method: boolean addAll(Collection c)
 Appends all the elements in the specified collection to
the end of the list
Example:
list2.add(a);
list2.add(b);
list2.add(c);
list.addAll(list); //adds all elements in list2 to
list
The List Interface
 Method: boolean addAll(int index, Collection c)
 Appends all the elements in the specified collection to
the specified position
Example:
list2.add(a);
list2.add(b);
list.add(a);
list.addAll(0,list); //adds all elements in list2 to
list before element a
The List Interface
 Method: void clear()
 Removes all elements from the list
Example:
list.add(a);
list.add(b);
list.clear(); //removes elements a and b
The List Interface
 Methods: boolean conatainsAll(Collection c)
 Returns true if the list contains all the element in the
collection
Example:
list2.add(a);
list2.add(b);
list.add(a);
list.add(b);
list2.containAll(list); //Returns true
The List Interface
 Method: equals(Object o)
 Compares the specific object with the list for equality
 Example
list.add(a);
list2.add(a);
list.equals(list2); //Returns true
///////////////////////////////////////////////////////////////////////////////
list.add(a);
list2.add(b);
list.equals(list2); //Returns false
The List Interface
 Method: Iterator iterator()
 Returns an iterator over the elements in the list in
proper sequence
 Example
Iterator iter = list.iterator(); //Creates an
iterator for list
The List Interface
 Method: ListIterator listIterator()
 Returns a list iterator over the elements in the list in
proper sequence
Example
ListIterator iter = list.listIterator(); //Creates a
list iterator
The List Interface
 Method: ListIterator listIterator(int index)
 Returns a list iterator over the elements in the list in
proper sequence, starting at the indicated position
Example
ListIterator iter = list.listIterator(2); //Creates
a list iterator starting at 2
The List Interface
 Method: boolean removeAll(collection c)
 Removes all of the elements specified from the list
Example
list2.add(a);
list2.add(b);
list2.add(c);
list.add(a);
list.add(b);
list2.removeAll(list); //Removes elements a and
b
The List Interface
 Method: boolean retainAll(collection c)
 Retains only the elements specified in the list
Example
list2.add(a);
list2.add(b);
list2.add(c);
list.add(a);
list2.retainAll(list); //Removes elements b and c
and keeps a
The List Interface
 Method: List subList(int fromIndex, int toIndex)
 Returns a view of the portion of the list between the
specified indexes
Example
list.add(a);
list.add(b);
list.add(c);
list.add(d);
list.subList(1,3); //returns a view of elements
b,c,d
The List Interface
 Method: Object[] toArray()
 Returns an array containg all of the elements in the
list
Example:
list.add(1);
list.add(2);
list.add(3);
int[] array = list.toArray(); //Creates an array
conating all of lists elements: 1, 2, and 3
The List Interface
 Method: Object[] toArray(Object a)
 Returns an array containg all of the elements in the
list, witht the runtime/ size of the array according to
the object
Example:
list.add(1);
list.add(2);
list.add(3);
int[] array = list.toArray(list); //Creats an array
conating all of lists elements: 1, 2, and 3 with size of list
Iterator Interface
 Method: boolean hasNext()
 Returns true if there are any items in the next position
Example:
While(iter.hasNext()) //Checks if there is a next
position
iter.next();
Iterator Interface
 Method: Object next()
 Returns the next item and moves to the next position
if hasNext is true
Example:
While(iter.hasNext())
iter.next(); //moves the position of the
array to the next spot
Iterator Interface
 Method: void remove()
 Removes item returned by the most recent call of next
Example:
While(iter.hasNext())
iter.next();
iter.remove(); //clears the list
Iterator Interface
 Method: boolean hasPrevious()
 Returns true if there are any items in the previous
position
Example:
While(iter.hasPrevious()) //Checks if there is a
previous position
iter.previous();
Iterator Interface
 Method: Object previous()
 Returns the previous item and moves to the position
before the current location if hasPrevious is true
Example:
While(iter.hasPrevious())
iter.previous(); //moves the position of
the array to the previous spot
Iterator Interface
 Method: Object nextIndex()
 Returns the value of the next index or -1 if none
Example:
list.add(a);
list.add(b);
ListIterator iter = list.listIterator();
iter.nextIndex(); //returns b
/////////////////////////////////////////////////////////////////
list.add(a);
ListIterator iter = list.listIterator();
iter.nextIndex(); //returns -1
Iterator Interface
 Method: Object previousIndex()
 Returns the value of the previous index or -1 if none
Example:
list.add(a);
ListIterator iter = list.listIterator();
iter.previousIndex(); //returns -1
/////////////////////////////////////////////////////////////////
list.add(a);
list.add(b);
iter.next();
ListIterator iter = list.listIterator();
iter.previousIndex(); //returns a
Iterator Interface
 Method: void add(Object 0)
 Inserts an object o at the current pposition
Example:
list.add(a);
list.add(b);
ListIterator iter = list.listIterator();
iter.add(z); // list looks like (z,a,b)
Iterator Interface
 Method: void set(Object o)
 Replaces the last item returned by next or previous
Example:
list.add(a);
list.add(b);
ListIterator iter = list.listIterator();
iter.next();
iter.set(z); //List looks like (a,z)

Java collections

  • 2.
    The List Interface Method: void add(int index, Object o)  Inserts specified element at specified position Example: list.add(4, a); //adds element a at position 4
  • 3.
    The List Interface Method: boolean add(Object o)  Adds specified element to the end of the list Example: list.add(a); //adds element a at the end of the list
  • 4.
    The List Interface Method: boolean contains(Object 0)  Returns true if the specified element is in the list Example: list.add(a); list.contains(a); //returns true //////////////////////////////////////////////////////////// list.add(b); list.contains(a); //returns false
  • 5.
    The List Interface Method: Object get(int index)  Returns the element at the specified position Example: list.add(a); list.add(b); list.get(1); //returns b
  • 6.
    The List Interface Method: int indexOf(object o);  Returns the index of the specified element or else -1 Example: list.add(a); list.add(b); list.add(c); list.indexOf(c); //returns 2
  • 7.
    The List Interface Method: int lastIndexOf(object o);  Returns the last index of the specified element or else -1 Example: list.add(a); list.add(b); list.add(a); list.indexOf(a); //returns 2
  • 8.
    The List Interface Method: Object remove(int index)  Removes the element at the specified index Example: list.add(a); list.add(b); list.add(c); list.remove(1); //removes element a
  • 9.
    The List Interface Method: boolean remove(Object o)  Removes the first occurrence of the specified element Example: list.add(a); list.add(b); list.add(a); list.remove(a); //removes element a at position 0
  • 10.
    The List Interface Method: object set(int index, Object element)  Replaces the element at the specified position and returns the old element Example: list.add(a); list.set(0, b); //replaces element a with b and returns element a
  • 11.
    The List Interface Method: boolean isEmpty()  Returns true if the method is empty Example list.isEmpty(); //retruns true //////////////////////////////////////////////////////////// list.add(a); list.isEmpty(); //returns false
  • 12.
    The List Interface Method: int size()  Returns the number of elements in the list Example: list.add(a); list.add(b); list.add(c); list.size(); //Returns 3
  • 13.
    The List Interface Method: boolean addAll(Collection c)  Appends all the elements in the specified collection to the end of the list Example: list2.add(a); list2.add(b); list2.add(c); list.addAll(list); //adds all elements in list2 to list
  • 14.
    The List Interface Method: boolean addAll(int index, Collection c)  Appends all the elements in the specified collection to the specified position Example: list2.add(a); list2.add(b); list.add(a); list.addAll(0,list); //adds all elements in list2 to list before element a
  • 15.
    The List Interface Method: void clear()  Removes all elements from the list Example: list.add(a); list.add(b); list.clear(); //removes elements a and b
  • 16.
    The List Interface Methods: boolean conatainsAll(Collection c)  Returns true if the list contains all the element in the collection Example: list2.add(a); list2.add(b); list.add(a); list.add(b); list2.containAll(list); //Returns true
  • 17.
    The List Interface Method: equals(Object o)  Compares the specific object with the list for equality  Example list.add(a); list2.add(a); list.equals(list2); //Returns true /////////////////////////////////////////////////////////////////////////////// list.add(a); list2.add(b); list.equals(list2); //Returns false
  • 18.
    The List Interface Method: Iterator iterator()  Returns an iterator over the elements in the list in proper sequence  Example Iterator iter = list.iterator(); //Creates an iterator for list
  • 19.
    The List Interface Method: ListIterator listIterator()  Returns a list iterator over the elements in the list in proper sequence Example ListIterator iter = list.listIterator(); //Creates a list iterator
  • 20.
    The List Interface Method: ListIterator listIterator(int index)  Returns a list iterator over the elements in the list in proper sequence, starting at the indicated position Example ListIterator iter = list.listIterator(2); //Creates a list iterator starting at 2
  • 21.
    The List Interface Method: boolean removeAll(collection c)  Removes all of the elements specified from the list Example list2.add(a); list2.add(b); list2.add(c); list.add(a); list.add(b); list2.removeAll(list); //Removes elements a and b
  • 22.
    The List Interface Method: boolean retainAll(collection c)  Retains only the elements specified in the list Example list2.add(a); list2.add(b); list2.add(c); list.add(a); list2.retainAll(list); //Removes elements b and c and keeps a
  • 23.
    The List Interface Method: List subList(int fromIndex, int toIndex)  Returns a view of the portion of the list between the specified indexes Example list.add(a); list.add(b); list.add(c); list.add(d); list.subList(1,3); //returns a view of elements b,c,d
  • 24.
    The List Interface Method: Object[] toArray()  Returns an array containg all of the elements in the list Example: list.add(1); list.add(2); list.add(3); int[] array = list.toArray(); //Creates an array conating all of lists elements: 1, 2, and 3
  • 25.
    The List Interface Method: Object[] toArray(Object a)  Returns an array containg all of the elements in the list, witht the runtime/ size of the array according to the object Example: list.add(1); list.add(2); list.add(3); int[] array = list.toArray(list); //Creats an array conating all of lists elements: 1, 2, and 3 with size of list
  • 26.
    Iterator Interface  Method:boolean hasNext()  Returns true if there are any items in the next position Example: While(iter.hasNext()) //Checks if there is a next position iter.next();
  • 27.
    Iterator Interface  Method:Object next()  Returns the next item and moves to the next position if hasNext is true Example: While(iter.hasNext()) iter.next(); //moves the position of the array to the next spot
  • 28.
    Iterator Interface  Method:void remove()  Removes item returned by the most recent call of next Example: While(iter.hasNext()) iter.next(); iter.remove(); //clears the list
  • 29.
    Iterator Interface  Method:boolean hasPrevious()  Returns true if there are any items in the previous position Example: While(iter.hasPrevious()) //Checks if there is a previous position iter.previous();
  • 30.
    Iterator Interface  Method:Object previous()  Returns the previous item and moves to the position before the current location if hasPrevious is true Example: While(iter.hasPrevious()) iter.previous(); //moves the position of the array to the previous spot
  • 31.
    Iterator Interface  Method:Object nextIndex()  Returns the value of the next index or -1 if none Example: list.add(a); list.add(b); ListIterator iter = list.listIterator(); iter.nextIndex(); //returns b ///////////////////////////////////////////////////////////////// list.add(a); ListIterator iter = list.listIterator(); iter.nextIndex(); //returns -1
  • 32.
    Iterator Interface  Method:Object previousIndex()  Returns the value of the previous index or -1 if none Example: list.add(a); ListIterator iter = list.listIterator(); iter.previousIndex(); //returns -1 ///////////////////////////////////////////////////////////////// list.add(a); list.add(b); iter.next(); ListIterator iter = list.listIterator(); iter.previousIndex(); //returns a
  • 33.
    Iterator Interface  Method:void add(Object 0)  Inserts an object o at the current pposition Example: list.add(a); list.add(b); ListIterator iter = list.listIterator(); iter.add(z); // list looks like (z,a,b)
  • 34.
    Iterator Interface  Method:void set(Object o)  Replaces the last item returned by next or previous Example: list.add(a); list.add(b); ListIterator iter = list.listIterator(); iter.next(); iter.set(z); //List looks like (a,z)