E
- The type of elements in the collection.public class BaseCollection<E> extends Object implements List<E>
Constructor and Description |
---|
BaseCollection()
Initializes a new instance of the collection that is empty.
|
BaseCollection(List<E> list)
Initializes a new instance of the collection as a wrapper for the specified list.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E e)
Appends the specified element to the end of this list (optional
operation).
|
void |
add(int index,
E element)
Inserts the specified element at the specified position in this list
(optional operation).
|
boolean |
addAll(Collection<? extends E> 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).
|
boolean |
addAll(int index,
Collection<? extends E> c)
Inserts all of the elements in the specified collection into this
list at the specified position (optional operation).
|
void |
clear()
Removes all of the elements from this list (optional operation).
|
protected void |
clearItems()
Removes all elements from the collection.
|
boolean |
contains(Object o)
Returns true if this list contains the specified element.
|
boolean |
containsAll(Collection<?> c)
Returns true if this list contains all of the elements of the
specified collection.
|
E |
get(int index)
Returns the element at the specified position in this list.
|
List<E> |
getItems()
Gets a list wrapper around the collection.
|
int |
indexOf(Object o)
Returns the index of the first occurrence of the specified element
in this list, or -1 if this list does not contain the element.
|
protected void |
insertItem(int index,
E item)
Inserts an element into the collection at the specified index.
|
boolean |
isEmpty()
Returns true if this list contains no elements.
|
Iterator<E> |
iterator()
Returns an iterator over the elements in this list in proper sequence.
|
int |
lastIndexOf(Object o)
Returns the index of the last occurrence of the specified element
in this list, or -1 if this list does not contain the element.
|
ListIterator<E> |
listIterator()
Returns a list iterator over the elements in this list (in proper
sequence).
|
ListIterator<E> |
listIterator(int index)
Returns a list iterator over the elements in this list (in proper
sequence), starting at the specified position in the list.
|
E |
remove(int index)
Removes the element at the specified position in this list (optional
operation).
|
boolean |
remove(Object o)
Removes the first occurrence of the specified element from this list,
if it is present (optional operation).
|
boolean |
removeAll(Collection<?> c)
Removes from this list all of its elements that are contained in the
specified collection (optional operation).
|
protected void |
removeItem(int index)
Removes the element at the specified index of the collection.
|
boolean |
retainAll(Collection<?> c)
Retains only the elements in this list that are contained in the
specified collection (optional operation).
|
E |
set(int index,
E element)
Replaces the element at the specified position in this list with the
specified element (optional operation).
|
protected void |
setItem(int index,
E element)
Replaces the element at the specified index.
|
int |
size()
Returns the number of elements in this list.
|
List<E> |
subList(int fromIndex,
int toIndex)
Returns a view of the portion of this list between the specified
fromIndex, inclusive, and toIndex, exclusive.
|
Object[] |
toArray()
Returns an array containing all of the elements in this list in proper
sequence (from first to last element).
|
<T> T[] |
toArray(T[] a)
Returns an array containing all of the elements in this list in
proper sequence (from first to last element); the runtime type of
the returned array is that of the specified array.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
equals, hashCode, replaceAll, sort, spliterator
parallelStream, removeIf, stream
public BaseCollection()
public boolean add(E e)
java.util.List
Lists that support this operation may place limitations on what elements may be added to this list. In particular, some lists will refuse to add null elements, and others will impose restrictions on the type of elements that may be added. List classes should clearly specify in their documentation any restrictions on what elements may be added.
add
in interface Collection<E>
add
in interface List<E>
e
- element to be appended to this listCollection.add(E)
)public void add(int index, E element)
java.util.List
public void clear()
java.util.List
protected void clearItems()
public boolean contains(Object o)
java.util.List
protected void insertItem(int index, E item)
index
- The zero-based index at which item should be inserted.item
- The object to insert.public boolean addAll(Collection<? extends E> c)
java.util.List
addAll
in interface Collection<E>
addAll
in interface List<E>
c
- collection containing elements to be added to this listList.add(Object)
public boolean addAll(int index, Collection<? extends E> c)
java.util.List
public boolean containsAll(Collection<?> c)
java.util.List
containsAll
in interface Collection<E>
containsAll
in interface List<E>
c
- collection to be checked for containment in this listList.contains(Object)
public E get(int index)
java.util.List
public int indexOf(Object o)
java.util.List
public boolean isEmpty()
java.util.List
public Iterator<E> iterator()
java.util.List
public int lastIndexOf(Object o)
java.util.List
lastIndexOf
in interface List<E>
o
- element to search forpublic ListIterator<E> listIterator()
java.util.List
listIterator
in interface List<E>
public ListIterator<E> listIterator(int index)
java.util.List
next
.
An initial call to previous
would
return the element with the specified index minus one.listIterator
in interface List<E>
index
- index of the first element to be returned from the
list iterator (by a call to next
)public boolean remove(Object o)
java.util.List
protected void removeItem(int index)
index
- The zero-based index of the element to remove.public E remove(int index)
java.util.List
public boolean removeAll(Collection<?> c)
java.util.List
removeAll
in interface Collection<E>
removeAll
in interface List<E>
c
- collection containing elements to be removed from this listList.remove(Object)
,
List.contains(Object)
public boolean retainAll(Collection<?> c)
java.util.List
retainAll
in interface Collection<E>
retainAll
in interface List<E>
c
- collection containing elements to be retained in this listList.remove(Object)
,
List.contains(Object)
public E set(int index, E element)
java.util.List
protected void setItem(int index, E element)
index
- The zero-based index of the element to replace.element
- The new value for the element at the specified index.public int size()
java.util.List
public List<E> subList(int fromIndex, int toIndex)
java.util.List
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, and all of the algorithms in the
Collections class can be applied to a subList.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 this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)
public Object[] toArray()
java.util.List
The returned array will be "safe" in that no references to it are maintained by this list. (In other words, this method must allocate a new array even if this list is backed by an array). The caller is thus free to modify the returned array.
This method acts as bridge between array-based and collection-based APIs.
toArray
in interface Collection<E>
toArray
in interface List<E>
Arrays.asList(Object[])
public <T> T[] toArray(T[] a)
java.util.List
If the list fits in the specified array with room to spare (i.e., the array has more elements than the list), the element in the array immediately following the end of the list is set to null. (This is useful in determining the length of the list only if the caller knows that the list does not contain any null elements.)
Like the List.toArray()
method, this method acts as bridge between
array-based and collection-based APIs. Further, this method allows
precise control over the runtime type of the output array, and may,
under certain circumstances, be used to save allocation costs.
Suppose x is a list known to contain only strings. The following code can be used to dump the list into a newly allocated array of String:
String[] y = x.toArray(new String[0]);
Note that toArray(new Object[0]) is identical in function to
toArray().toArray
in interface Collection<E>
toArray
in interface List<E>
T
- the runtime type of the array to contain the collectiona
- the array into which the elements of this list are to
be stored, if it is big enough; otherwise, a new array of the
same runtime type is allocated for this purpose.