| Overview | Package | Class | Tree | Index | Help | |||
| PREV CLASS | NEXT CLASS | FRAMES | NO FRAMES | ||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||
Unlike Sets, Lists typically allow duplicate elements. More formally,
Lists typically allow pairs of elements e1 and e2
such that e1.equals(e2), and they typically allow multiple
null elements if they allow null elements at all. It is not inconceivable
that someone might wish to implement a List that prohibits duplicates,
by throwing runtime exceptions when the user attempts to insert them,
but we expect this usage to be rare.
The List interface places additional stipulations, beyond those specified in the Collection Interface, on the contracts of the iterator, add, remove, equals, and hashCode methods. Declarations for other inherited methods are also included here for convenience.
Lists provide four methods for positional (indexed) access to List elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (LinkedList, for example). Thus, iterating over the elements in a List is typically preferable to indexing through it if the caller does not know the implementation.
List provides a special Iterator, called a ListIterator, that allows element insertion and replacement, and bidirectional access in addition to the normal Iterator operations. A method is provided to obtain a List iterator that starts at a specified position in the List.
List provides four methods to search for a specified object. From a performance standpoint, these methods should be used with caution. In many implementations, they will perform costly linear searches.
List provides two methods to efficiently insert and remove multiple elements at an arbitrary point in the List.
Implemented by ArrayList, LinkedList, Vector, and returned by Arrays.asList, Collection.subList and Collections.ncopies.
| Method Summary | |
| boolean | addAll(Collection c)
Appends all of the elements in the specified Collection to the end of this this List, in the order that they are returned by the specified Collection's Iterator (optional operation). |
| boolean | addAll(int index,
Collection c)
Inserts all of the elements in the specified Collection into this List at the specified position (optional operation). |
| boolean | add(java.lang.Object o)
Appends the specified element to the end of this List (optional operation). |
| void | add(int index,
java.lang.Object element)
Inserts the specified element at the specified position in this List (optional operation). |
| void | clear()
Removes all of the elements from this List (optional operation). |
| boolean | containsAll(Collection c)
Returns true if this List contains all of the elements of the specified Collection. |
| boolean | contains(java.lang.Object o)
Returns true if this List contains the specified element. |
| boolean | equals(java.lang.Object o)
Compares the specified Object with this List for equality. |
| java.lang.Object | get(int index)
Returns the element at the specified position in this List. |
| int | hashCode()
Returns the hash code value for this List. |
| int | indexOf(java.lang.Object o)
Returns the index in this List of the first occurrence of the specified element, or -1 if the List does not contain this element. |
| boolean | isEmpty()
Returns true if this List contains no elements. |
| Iterator | iterator()
Returns an Iterator over the elements in this List in proper sequence. |
| int | lastIndexOf(java.lang.Object o)
Returns the index in this List of the last occurrence of the specified element, or -1 if the List does not contain this element. |
| ListIterator | listIterator()
Returns a ListIterator of the elements in this List (in proper sequence). |
| ListIterator | listIterator(int index)
Returns a ListIterator of the elements in this List (in proper sequence), starting at the specified position in the List. |
| boolean | removeAll(Collection c)
Removes from this List all of its elements that are contained in the specified Collection (optional operation). |
| boolean | remove(java.lang.Object o)
Removes the first occurrence of the specified element in this List (optional operation). |
| java.lang.Object | remove(int index)
Removes the element at the specified position in this List (optional operation). |
| boolean | retainAll(Collection c)
Retains only the elements in this List that are contained in the specified Collection (optional operation). |
| java.lang.Object | set(int index,
java.lang.Object element)
Replaces the element at the specified position in this List with the specified element (optional operation). |
| int | size()
Returns the number of elements in this List. |
| List | subList(int fromIndex,
int toIndex)
Returns a view of the portion of this List between fromIndex, inclusive, and toIndex, exclusive. |
| java.lang.Object[] | toArray()
Returns an array containing all of the elements in this List. |
| Method Detail |
public int size()
public boolean isEmpty()
public boolean contains(java.lang.Object o)
e such that (o==null ? e==null :
o.equals(e)).
o
- element whose presence in this List is to be tested.public Iterator iterator()
public java.lang.Object[] toArray()
public boolean add(java.lang.Object o)
o
- element to be appended to this List.public boolean remove(java.lang.Object o)
(o==null ? get(i)==null : o.equals(get(i))) (if
such an element exists).
o
- element to be removed from this List, if present.public boolean containsAll(Collection c)
public boolean addAll(Collection c)
public boolean addAll(int index,
Collection c)
index
- index at which to insert first element from the specified
Collection.
c
- elements to be inserted into this List.public boolean removeAll(Collection c)
public boolean retainAll(Collection c)
public void clear()
public boolean equals(java.lang.Object o)
e1 and e2 are
equal if (e1==null ? e2==null : e1.equals(e2)).)
In other words, two Lists are defined to be equal if they contain the
same elements in the same order.
o
- the Object to be compared for equality with this List.public int hashCode()
hashCode = 1;
Iterator i = list.iterator();
while (i.hasNext()) {
Object obj = i.next();
hashCode = 31*hashCode + (obj==null ? 0 : obj.hashCode());
}
This ensures that list1.equals(list2) implies that
list1.hashCode()==list2.hashCode() for any two Lists,
list1 and list2, as required by the general
contract of Object.hashCode.public java.lang.Object get(int index)
index
- index of element to return.
public java.lang.Object set(int index,
java.lang.Object element)
index
- index of element to replace.
element
- element to be stored at the specified position.
public void add(int index,
java.lang.Object element)
index
- index at which the specified element is to be inserted.
element
- element to be inserted.public java.lang.Object remove(int index)
index
- the index of the element to removed.public int indexOf(java.lang.Object o)
(o==null ? get(i)==null : o.equals(get(i))),
or -1 if there is no such index.
o
- element to search for.public int lastIndexOf(java.lang.Object o)
(o==null ? get(i)==null : o.equals(get(i))),
or -1 if there is no such index.
o
- element to search for.public ListIterator listIterator()
public ListIterator listIterator(int index)
index
- index of first element to be returned from the
ListIterator (by a call to getNext).
public List subList(int fromIndex,
int toIndex)
This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a List can be used as a range operation by passing a subList view instead of a whole List. For example, the following idiom removes a range of elements from a List:
list.subList(from, to).clear();Similar idioms may be constructed for indexOf and lastIndexOf. The semantics of the List returned by this method become undefined if the backing list (i.e., this List) is structurally modified in any way other than via the returned List. (Structural modifications are those that change the size of the List, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)
fromIndex
- low endpoint (inclusive) of the subList.
toKey
- high endpoint (exclusive) of the subList.| Overview | Package | Class | Tree | Index | Help | |||
| PREV CLASS | NEXT CLASS | FRAMES | NO FRAMES | ||
| SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||