-
- Type Parameters:
T
- the entity type of the sequence
- All Superinterfaces:
Collection<T>
,Foldable<T>
,java.lang.Iterable<T>
,java.io.Serializable
,Streamable<T>
,Traversable<T>
,Value<T>
public interface List<T> extends Collection<T>
- Since:
- 0.0.5
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description List<T>
append(T value)
Create a new list containing all elements in this instance and appending the providedvalue
to the end of the new list.List<T>
distinctBy(java.util.Comparator<T> comparator)
Creates a distinct list based upon the provided comparator.List<T>
filter(java.util.function.Predicate<T> predicate)
Filter out an element if it does not match the suppliedpredicate
.int
firstIndexWhere(java.util.function.Predicate<T> predicate)
Search the collections for the first element matching the providedPredicate
and return the index position of that element.T
get(int index)
Get the element at the location ofindex
<K> Map<K,? extends List<T>>
groupBy(java.util.function.Function<? super T,? extends K> keyGenerator)
Generate a new sequence using thekeyGenerator
.default int
indexOf(java.lang.Object lookFor)
Find the index for the provided element, will return-1
if the element is not present in the list.<U> List<U>
map(java.util.function.Function<T,U> mapper)
Perform a mapping operation on the elements in the stream.default double
median()
Computes the median value of the set.List<T>
orElse(java.lang.Iterable<? extends T> other)
Returns eitherthis
if it is non empty, otherwise will return the providedother
.List<T>
orElse(java.util.function.Supplier<? extends java.lang.Iterable<? extends T>> supplier)
Returns eitherthis
if it is non empty, otherwise the provided supplier is evaluated and returned.Pipeline<T>
pipeline()
Create a pipeline for the current list.List<T>
reject(java.util.function.Predicate<T> predicate)
Return a list that removes all elements that match thepredicate
provided.List<T>
remove(int index)
Removes an element from the list and returns a new instance of the list.List<T>
sorted()
Sorts the specified array of objects into ascending order, according to the natural ordering of its elements.List<T>
tail()
Build a new collection with all elements except for the head.List<T>
union(java.lang.Iterable<T> iterable)
Create a new sequence with all elements of this sequence combined with the elements of the provided iterable.-
Methods inherited from interface com.jongsoft.lang.collection.Collection
containsAll, count, first, foldLeft, foldRight, get, head, isEmpty, isSingleValued, last, reduceLeft, size, split, summing
-
Methods inherited from interface com.jongsoft.lang.collection.Traversable
average, iterator, sum
-
-
-
-
Method Detail
-
append
List<T> append(T value)
Create a new list containing all elements in this instance and appending the providedvalue
to the end of the new list.Example:
// will result in a list with 2, 3, 4, 1 List(2, 3, 4).append(1)
- Parameters:
value
- the value to append to the list- Returns:
- the new list with the value appended
-
distinctBy
List<T> distinctBy(java.util.Comparator<T> comparator)
Creates a distinct list based upon the provided comparator. If a duplicate is found only the first match will be included in the returned list.- Parameters:
comparator
- the comparator to use for distinct check- Returns:
- the distinct list
-
firstIndexWhere
int firstIndexWhere(java.util.function.Predicate<T> predicate)
Search the collections for the first element matching the providedPredicate
and return the index position of that element.- Parameters:
predicate
- the predicate to match- Returns:
- index of the found element, -1 if none found
-
filter
List<T> filter(java.util.function.Predicate<T> predicate)
Description copied from interface:Streamable
Filter out an element if it does not match the suppliedpredicate
. This operation will iterate over all elements and return a new set containing only the elements where the predicate returnstrue
for.- Specified by:
filter
in interfaceCollection<T>
- Specified by:
filter
in interfaceStreamable<T>
- Specified by:
filter
in interfaceTraversable<T>
- Parameters:
predicate
- the predicate to apply to the contents of this- Returns:
- the filtered value
-
get
T get(int index)
Get the element at the location ofindex
- Parameters:
index
- the index of the element in the list to get- Returns:
- the element at the provided index
- Throws:
java.lang.IndexOutOfBoundsException
- ifindex
is greater then theCollection.size()
- 1
-
groupBy
<K> Map<K,? extends List<T>> groupBy(java.util.function.Function<? super T,? extends K> keyGenerator)
Generate a new sequence using thekeyGenerator
. Calling this operation will create a new map grouping the elements by the generator provided.Example:
// This will result in Map(1 -> List(1, 11, 21), 2 -> List(2, 12, 22)) Sequence(1, 2, 11, 12, 21, 22) .groupBy(x -> x % 10);
- Type Parameters:
K
- the type of the key- Parameters:
keyGenerator
- the generator to use for creating keys- Returns:
- the new map created using the generator
- Throws:
java.lang.NullPointerException
- ifkeyGenerator
is null
-
indexOf
default int indexOf(java.lang.Object lookFor)
Find the index for the provided element, will return-1
if the element is not present in the list.- Parameters:
lookFor
- the element to look for- Returns:
- the index of the element, or
-1
if none found
-
map
<U> List<U> map(java.util.function.Function<T,U> mapper)
Description copied from interface:Streamable
Perform a mapping operation on the elements in the stream.This operation will loop over all elements in the stream and apply the
mapper
method. The mapped values will be returned in as a new stream of elements.- Specified by:
map
in interfaceCollection<T>
- Specified by:
map
in interfaceStreamable<T>
- Specified by:
map
in interfaceTraversable<T>
- Type Parameters:
U
- the type of object expected as a result- Parameters:
mapper
- the mapping functionality- Returns:
- the mapped object
-
median
default double median()
Computes the median value of the set. This will only work if all elements in the set are of the typeNumber
.- Returns:
- The median value.
-
orElse
List<T> orElse(java.lang.Iterable<? extends T> other)
Description copied from interface:Traversable
Returns eitherthis
if it is non empty, otherwise will return the providedother
.- Specified by:
orElse
in interfaceCollection<T>
- Specified by:
orElse
in interfaceTraversable<T>
- Parameters:
other
- the alternative- Returns:
- this
Traversable
if non empty, orother
-
orElse
List<T> orElse(java.util.function.Supplier<? extends java.lang.Iterable<? extends T>> supplier)
Description copied from interface:Traversable
Returns eitherthis
if it is non empty, otherwise the provided supplier is evaluated and returned.- Specified by:
orElse
in interfaceCollection<T>
- Specified by:
orElse
in interfaceTraversable<T>
- Parameters:
supplier
- the supplier to generate the other- Returns:
- this (@code Traversable} if non empty, otherwise other
-
pipeline
Pipeline<T> pipeline()
Create a pipeline for the current list. A pipeline can be used to create a set of lazy operations on the list that will only get executed once the pipe is terminated.- Returns:
- the pipeline for this list
-
reject
List<T> reject(java.util.function.Predicate<T> predicate)
Description copied from interface:Traversable
Return a list that removes all elements that match thepredicate
provided.- Specified by:
reject
in interfaceCollection<T>
- Specified by:
reject
in interfaceTraversable<T>
- Parameters:
predicate
- the predicate to use- Returns:
- the elements that do not match the
predicate
-
remove
List<T> remove(int index)
Removes an element from the list and returns a new instance of the list.- Parameters:
index
- the index of the element to be removed- Returns:
- the new instance of the list without the element at the provided index
- Throws:
java.lang.IndexOutOfBoundsException
- ifindex
is not between the 0 and Collection.size()
-
sorted
List<T> sorted()
Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. All elements in the array must implement the Comparable interface. Furthermore, all elements in the array must be mutually comparable (that is, e1.compareTo(e2) must not throw a ClassCastException for any elements e1 and e2 in the array).This sort is guaranteed to be stable: equal elements will not be reordered as a result of the sort. Implementation note: This implementation is a stable, adaptive, iterative mergesort that requires far fewer than n lg(n) comparisons when the input array is partially sorted, while offering the performance of a traditional mergesort when the input array is randomly ordered. If the input array is nearly sorted, the implementation requires approximately n comparisons. Temporary storage requirements vary from a small constant for nearly sorted input arrays to n/2 object references for randomly ordered input arrays.
The implementation takes equal advantage of ascending and descending order in its input array, and can take advantage of ascending and descending order in different parts of the same input array. It is well-suited to merging two or more sorted arrays: simply concatenate the arrays and sort the resulting array.
- Returns:
- the sorted set
-
tail
List<T> tail()
Description copied from interface:Collection
Build a new collection with all elements except for the head. If there is only one element present then an empty collection will be returned. If the operation is called on an empty collection anNoSuchElementException
will be thrown.- Specified by:
tail
in interfaceCollection<T>
- Returns:
- a collection containing the tail
-
union
List<T> union(java.lang.Iterable<T> iterable)
Create a new sequence with all elements of this sequence combined with the elements of the provided iterable. The elements in this sequence will be added before the providediterable
in the returned sequence.Example:
// the example would be a List(1, 2, 3, 4) List<Integer> result = List(1, 2).union(List(3, 4));
- Parameters:
iterable
- the elements to be added- Returns:
- the new list containing a union between this and the iterable
-
-