public class ListBuilder extends Object implements List
Modifier and Type | Method and Description |
---|---|
void |
add(int index,
Object element)
Inserts the specified element at the specified position in this list
(optional operation).
|
boolean |
add(Object e)
Appends the specified element to the end of this list (optional
operation).
|
boolean |
addAll(Collection 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 c)
Inserts all of the elements in the specified collection into this
list at the specified position (optional operation).
|
ListBuilder |
append(Object e)
Adds an element to the list using the
add(java.lang.Object) method, and returns this builder. |
void |
clear()
Removes all of the elements from this list (optional operation).
|
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.
|
boolean |
equals(Object obj)
Indicates whether some other object is "equal to" this one.
|
Object |
get(int index)
Returns the element at the specified position in this list.
|
int |
hashCode()
Returns a hash code value for the object.
|
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.
|
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(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 |
listIterator()
Returns a list iterator over the elements in this list (in proper
sequence).
|
ListIterator |
listIterator(int index)
Returns a list iterator over the elements in this list (in proper
sequence), starting at the specified position in the list.
|
Object |
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).
|
boolean |
retainAll(Collection c)
Retains only the elements in this list that are contained in the
specified collection (optional operation).
|
Object |
set(int index,
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 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).
|
Object[] |
toArray(Object[] 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.
|
String |
toString()
Returns a string representation of the object.
|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
replaceAll, sort, spliterator
parallelStream, removeIf, stream
public ListBuilder append(Object e)
add(java.lang.Object)
method, and returns this builder.e
- element to be appended to this listpublic int size()
size
in interface Collection
size
in interface List
public boolean isEmpty()
isEmpty
in interface Collection
isEmpty
in interface List
public boolean contains(Object o)
contains
in interface Collection
contains
in interface List
o
- element whose presence in this list is to be testedpublic Iterator iterator()
public Object[] toArray()
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
toArray
in interface List
Arrays.asList(Object[])
public Object[] toArray(Object[] a)
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
toArray
in interface List
a
- 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.public boolean add(Object e)
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
add
in interface List
e
- element to be appended to this listCollection.add(E)
)public boolean remove(Object o)
remove
in interface Collection
remove
in interface List
o
- element to be removed from this list, if presentpublic boolean containsAll(Collection c)
containsAll
in interface Collection
containsAll
in interface List
c
- collection to be checked for containment in this listList.contains(Object)
public boolean addAll(Collection c)
addAll
in interface Collection
addAll
in interface List
c
- collection containing elements to be added to this listList.add(Object)
public boolean addAll(int index, Collection c)
public boolean removeAll(Collection c)
removeAll
in interface Collection
removeAll
in interface List
c
- collection containing elements to be removed from this listList.remove(Object)
,
List.contains(Object)
public boolean retainAll(Collection c)
retainAll
in interface Collection
retainAll
in interface List
c
- collection containing elements to be retained in this listList.remove(Object)
,
List.contains(Object)
public void clear()
clear
in interface Collection
clear
in interface List
public Object get(int index)
public Object set(int index, Object element)
public void add(int index, Object element)
public Object remove(int index)
public int indexOf(Object o)
public int lastIndexOf(Object o)
lastIndexOf
in interface List
o
- element to search forpublic ListIterator listIterator()
listIterator
in interface List
public ListIterator listIterator(int index)
next
.
An initial call to previous
would
return the element with the specified index minus one.listIterator
in interface List
index
- index of the first element to be returned from the
list iterator (by a call to next
)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, 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 boolean equals(Object obj)
The equals
method implements an equivalence relation
on non-null object references:
x
, x.equals(x)
should return
true
.
x
and y
, x.equals(y)
should return true
if and only if
y.equals(x)
returns true
.
x
, y
, and z
, if
x.equals(y)
returns true
and
y.equals(z)
returns true
, then
x.equals(z)
should return true
.
x
and y
, multiple invocations of
x.equals(y)
consistently return true
or consistently return false
, provided no
information used in equals
comparisons on the
objects is modified.
x
,
x.equals(null)
should return false
.
The equals
method for class Object
implements
the most discriminating possible equivalence relation on objects;
that is, for any non-null reference values x
and
y
, this method returns true
if and only
if x
and y
refer to the same object
(x == y
has the value true
).
Note that it is generally necessary to override the hashCode
method whenever this method is overridden, so as to maintain the
general contract for the hashCode
method, which states
that equal objects must have equal hash codes.
equals
in interface Collection
equals
in interface List
equals
in class Object
obj
- the reference object with which to compare.true
if this object is the same as the obj
argument; false
otherwise.Object.hashCode()
,
HashMap
public int hashCode()
HashMap
.
The general contract of hashCode
is:
hashCode
method
must consistently return the same integer, provided no information
used in equals
comparisons on the object is modified.
This integer need not remain consistent from one execution of an
application to another execution of the same application.
equals(Object)
method, then calling the hashCode
method on each of
the two objects must produce the same integer result.
Object.equals(java.lang.Object)
method, then calling the hashCode
method on each of the
two objects must produce distinct integer results. However, the
programmer should be aware that producing distinct integer results
for unequal objects may improve the performance of hash tables.
As much as is reasonably practical, the hashCode method defined by
class Object
does return distinct integers for distinct
objects. (This is typically implemented by converting the internal
address of the object into an integer, but this implementation
technique is not required by the
Java™ programming language.)
hashCode
in interface Collection
hashCode
in interface List
hashCode
in class Object
Object.equals(java.lang.Object)
,
System.identityHashCode(java.lang.Object)
public String toString()
toString
method returns a string that
"textually represents" this object. The result should
be a concise but informative representation that is easy for a
person to read.
It is recommended that all subclasses override this method.
The toString
method for class Object
returns a string consisting of the name of the class of which the
object is an instance, the at-sign character `@
', and
the unsigned hexadecimal representation of the hash code of the
object. In other words, this method returns a string equal to the
value of:
getClass().getName() + '@' + Integer.toHexString(hashCode())