public final class OneItemList<E> extends AbstractList<E> implements RandomAccess
List
interface. Implements all
optional list operations, except those which would change the number of list
items. Permits all elements, including null
.
All operations run in constant time.
modCount
Constructor and Description |
---|
OneItemList(E item)
Constructs a list with exactly the specified object as its content.
|
Modifier and Type | Method and Description |
---|---|
boolean |
add(E e)
Since instances of this class cannot be modified in size, this method
always throws an Exception.
|
boolean |
contains(Object o)
Returns
true if this list contains the specified element. |
E |
get(int index)
Returns the element at the specified position in this list.
|
boolean |
isEmpty()
Returns
false . |
Iterator<E> |
iterator()
Returns an iterator over the one element in this list.
|
ListIterator<E> |
listIterator(int index)
Returns an iterator over the one element in this list.
|
boolean |
removeAll(Collection<?> c)
Since instances of this class cannot be modified in size, this method
always throws an Exception.
|
boolean |
retainAll(Collection<?> c)
Since instances of this class cannot be modified in size, this method
always throws an Exception.
|
int |
size()
Returns
1 . |
Object[] |
toArray()
Returns an array containing the one element in this list.
|
<T> T[] |
toArray(T[] a)
Returns an array containing the one element in this list; the runtime
type of the returned array is that of the specified array.
|
String |
toString()
Returns a string representation of this collection.
|
add, addAll, clear, equals, hashCode, indexOf, lastIndexOf, listIterator, remove, removeRange, set, subList
addAll, containsAll, remove
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
addAll, containsAll, remove, replaceAll, sort, spliterator
parallelStream, removeIf, stream
public OneItemList(E item)
item
- the single list item to be the content of this listpublic boolean add(E e)
add
in interface Collection<E>
add
in interface List<E>
add
in class AbstractList<E>
UnsupportedOperationException
public E get(int index)
get
in interface List<E>
get
in class AbstractList<E>
index
- index of the element to returnIndexOutOfBoundsException
- unless index == 0
public ListIterator<E> listIterator(int index)
listIterator
in interface List<E>
listIterator
in class AbstractList<E>
index
- irst element to be returned from the list iterator (by a
call to the next
method)IndexOutOfBoundsException
- unless index == 0
public int size()
1
.size
in interface Collection<E>
size
in interface List<E>
size
in class AbstractCollection<E>
public boolean isEmpty()
false
.isEmpty
in interface Collection<E>
isEmpty
in interface List<E>
isEmpty
in class AbstractCollection<E>
false
public boolean contains(Object o)
true
if this list contains the specified element.
More formally, returns true
if and only if this list's one
element is identical to the object specified.contains
in interface Collection<E>
contains
in interface List<E>
contains
in class AbstractCollection<E>
o
- element whose presence in this list is to be testedtrue
if this list contains the specified elementpublic Object[] toArray()
toArray
in interface Collection<E>
toArray
in interface List<E>
toArray
in class AbstractCollection<E>
1
containing exactly the one
element in this list.Arrays.asList(Object[])
public <T> T[] toArray(T[] a)
1
, it is returned therein.
Otherwise, a new array is allocated with the runtime type of the
specified array and the length 1
.toArray
in interface Collection<E>
toArray
in interface List<E>
toArray
in class AbstractCollection<E>
a
- the array into which the element 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.ArrayStoreException
- if the runtime type of the specified array
is not a supertype of the runtime type of the element in this listNullPointerException
- if the specified array is nullpublic boolean removeAll(Collection<?> c)
removeAll
in interface Collection<E>
removeAll
in interface List<E>
removeAll
in class AbstractCollection<E>
UnsupportedOperationException
public boolean retainAll(Collection<?> c)
retainAll
in interface Collection<E>
retainAll
in interface List<E>
retainAll
in class AbstractCollection<E>
UnsupportedOperationException
public String toString()
"[]"
). Elements are converted to strings as by
String.valueOf(Object)
.toString
in class AbstractCollection<E>