java.lang.Object
me.sbasalaev.collection.Collection<T>
me.sbasalaev.collection.List<T>
- All Implemented Interfaces:
Cloneable,Iterable<T>,Traversable<T>
- Direct Known Subclasses:
MutableList
Sequence of elements numbered by integer indices.
An implementation of the list must provide get() and size().
The default iterator implementation uses these methods.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintbinarySearch(ToIntFunction<@Out T> comparator) Finds item using binary search assuming elements are in increasing order.clone()Returns shallow immutable copy of this list.static <T> List<T> concatenated(List<? extends T>... lists) Concatenates several lists together.static <T> List<T> empty()Empty list.booleanCompares two objects for equality.Returns new list that contains only elements of this list satisfying given condition.intThe index of the first element satisfying given condition.intThe index of the first element satisfying given condition.intfindLastIndex(Predicate<? super @Out T> condition) The index of the last element satisfying given condition.first()The first element of this list.voidforEachIndexed(ObjIntConsumer<? super @Out T> action) Performs given action for each element of this list.voidforEachPair(BiConsumer<? super @Out T, ? super @Out T> action) Performs given action for all pairs of elements in this list.from(int offset) A view of this list that contains only elements starting from given offset.static <T> List<T> List view of given Java list.abstract Tget(int index) Element at the specified index of the list.inthashCode()Returns hash code for the list.indexed()A view of this list where each element is coupled with the corresponding index.iterator()Iterator of this list.last()The last element of this list.intThe last valid index in this list.<R> List<R> Returns view of this list with given mapping applied to all elements.<R> List<R> Returns new list with given mapping applied to all elements.static <T> List<T> of(T... elements) List of given elements.<R> Traversable<R> Traverses all pairs of elements in this list.static <T> List<T> repeat(T element, int times) List that repeats the same element given number of times.reversed()A view of this list that contains the same elements in the reversed order.Creates aSpliteratorover elements of this list.take(int limit) A view of this list that contains no more thanlimitelements.toJava()A view of this list as Java list.<U,R> List <R> zip(List<U> other, BiFunction<? super @Out T, ? super U, ? extends R> zipper) A combined view ofthisandotherlists using given zipper.Methods inherited from class me.sbasalaev.collection.Collection
count, fillArray, isEmpty, nonEmpty, size, stream, toArray, toArray, toList, toSet, toString
-
Constructor Details
-
List
public List()Constructor for subclasses.
-
-
Method Details
-
empty
Empty list. -
of
List of given elements. -
concatenated
Concatenates several lists together. -
fromJava
List view of given Java list. -
repeat
List that repeats the same element given number of times. -
get
Element at the specified index of the list.- Throws:
IndexOutOfBoundsException- ifindexis negative or ≥Collection.size().
-
lastIndex
public int lastIndex()The last valid index in this list.- Returns:
size()-1.
-
first
The first element of this list.- Throws:
NoSuchElementException- if the list is empty.- See Also:
-
last
The last element of this list.- Throws:
NoSuchElementException- if the list is empty.- See Also:
-
findIndex
The index of the first element satisfying given condition. Returns-1if there is no such element. -
findIndex
The index of the first element satisfying given condition. Returns-1if there is no such element. -
findLastIndex
The index of the last element satisfying given condition. Returns-1if there is no such element. -
binarySearch
Finds item using binary search assuming elements are in increasing order. -
indexed
A view of this list where each element is coupled with the corresponding index. The returned list is a view that is affected immediately by the changes to this list. -
forEachIndexed
Performs given action for each element of this list. The consumer for this method accepts item index as the second argument.- Since:
- 4.0
-
pairs
Traverses all pairs of elements in this list. The resulting traversable returns elements produced bycombiner.apply(get(i), get(j))
for all indicesi, jsuch that0 <= i < j < size()
In particular, the traversable is empty if the list has less than two elements.- Since:
- 4.0
-
forEachPair
Performs given action for all pairs of elements in this list. The action is called asaction.accept(get(i), get(j))
for all indicesi, jsuch that0 <= i < j < size()
In particular, no action is performed if the list has less than two elements.- Since:
- 4.0
-
from
A view of this list that contains only elements starting from given offset. If this list contains no more thanoffsetelements the returned view is empty. The returned list is a view that is affected immediately by the changes to this list.- Parameters:
offset- non-negative offset from the start of the list.- Throws:
IllegalArgumentException- if the offset is negative.
-
take
A view of this list that contains no more thanlimitelements. The returned list is a view that is affected immediately by the changes to this list.- Parameters:
limit- maximum number of elements to take.- Throws:
IllegalArgumentException- if given limit is negative.- See Also:
-
reversed
A view of this list that contains the same elements in the reversed order. The returned list is a view that is affected immediately by the changes to this list. -
zip
A combined view ofthisandotherlists using given zipper. The returned list is a view that is affected immediately by the changes tothisandotherlists. Size of the view is the minimum of sizes of two lists and the element at each index is a combination byzipperof the elements ofthisandotherat the same index. -
toJava
A view of this list as Java list. -
iterator
Iterator of this list. The iterator returns elements of this list in order, starting at index 0.The default implementation of this method returns the iterator that is not aware of the changes made to this list.
-
spliterator
Creates aSpliteratorover elements of this list. The spliterator reportsSpliterator.NONNULL,Spliterator.SIZEDandSpliterator.ORDERED.- Specified by:
spliteratorin interfaceIterable<T>- Specified by:
spliteratorin classCollection<T>
-
clone
Returns shallow immutable copy of this list. May return the same instance if the list is immutable.- Specified by:
clonein classCollection<T>
-
map
Returns view of this list with given mapping applied to all elements. The returned list is a view that is affected immediately by the changes to this list.- See Also:
-
mapped
Returns new list with given mapping applied to all elements. The returned list is immutable and is unaffected by the changes to this list.- Specified by:
mappedin classCollection<T>
-
filtered
Returns new list that contains only elements of this list satisfying given condition. The returned list is immutable and is unaffected by the changes to this list.- Specified by:
filteredin classCollection<T>
-
equals
Compares two objects for equality. Two lists are equal if they have the same size and the corresponding pairs of elements are equal.- Specified by:
equalsin classCollection<T>
-
hashCode
public int hashCode()Returns hash code for the list. The hash code for the list is defined to be equal tofold(1, (hash, item) -> hash*31 + item.hashCode());
This means that the hash code for the list is compatible with Java definitions forList.hashCode(),Arrays.hashCode(java.lang.Object[])andObjects.hash(java.lang.Object...).- Specified by:
hashCodein classCollection<T>
-