-
- Type Parameters:
T
- the type of the elements
- All Superinterfaces:
Foldable<T>
,java.lang.Iterable<T>
,java.io.Serializable
,Streamable<T>
,Traversable<T>
,Value<T>
- All Known Subinterfaces:
List<T>
,Map<K,T>
,Sequence<T>
,Set<T>
,Tree.NodeCollection<T>
public interface Collection<T> extends Traversable<T>
The collection interface enables basic operations that allow access to the elements.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description default boolean
containsAll(java.lang.Iterable<? extends T> elements)
Checks if all elements in the provided iterable are contained in this collection.default int
count(java.util.function.Predicate<T> predicate)
Count all elements that match the provided predicate.Collection<T>
filter(java.util.function.Predicate<T> predicate)
Filter out an element if it does not match the suppliedpredicate
.default Optional<T>
first(java.util.function.Predicate<T> predicate)
Find the first match in the elements using the providedPredicate
.default <U> U
foldLeft(U start, java.util.function.BiFunction<? super U,? super T,? extends U> combiner)
Folds the elements from the left, starting with thestart
value and combining the result by successively calling thecombiner
operation.default <U> U
foldRight(U start, java.util.function.BiFunction<? super T,? super U,? extends U> combiner)
Folds the elements from the right, starting with thestart
value and combining the result by successively calling thecombiner
.default T
get()
Fetch the head of the collection and return it.T
head()
Fetch the head of the collection and return it.default boolean
isEmpty()
Convenience method to see if the current list is empty or not.default boolean
isSingleValued()
Is this instance single-valued or not.default Optional<T>
last(java.util.function.Predicate<T> predicate)
Find the last match in the Iterator using the providedPredicate
.<U> Collection<U>
map(java.util.function.Function<T,U> mapper)
Perform a mapping operation on the elements in the stream.Collection<T>
orElse(java.lang.Iterable<? extends T> other)
Returns eitherthis
if it is non empty, otherwise will return the providedother
.Collection<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.default T
reduceLeft(java.util.function.BiFunction<? super T,? super T,? extends T> reducer)
Reduce the collection from the left, starting with the first element in the collection and successively calling thereducer
until there are no more elements.Collection<T>
reject(java.util.function.Predicate<T> predicate)
Return a list that removes all elements that match thepredicate
provided.int
size()
Get the amount of elements contained in the collection.Pair<? extends Collection<T>,? extends Collection<T>>
split(java.util.function.Predicate<T> predicate)
The split operation is an execution that combines thereject(Predicate)
and thefilter(Predicate)
methods into one.default long
summing(java.util.function.BiFunction<java.lang.Long,T,java.lang.Long> accumulator)
Sum all elements using the provided accumulator.Collection<T>
tail()
Build a new collection with all elements except for the head.-
Methods inherited from interface com.jongsoft.lang.collection.Traversable
average, iterator, sum
-
-
-
-
Method Detail
-
containsAll
default boolean containsAll(java.lang.Iterable<? extends T> elements)
Checks if all elements in the provided iterable are contained in this collection.This is a convenience method that uses the
Value.contains(Object)
to verify existence.- Parameters:
elements
- the elements that should be present- Returns:
- true if all elements are present, false otherwise
- See Also:
Value.contains(Object)
-
count
default int count(java.util.function.Predicate<T> predicate)
Count all elements that match the provided predicate.- Parameters:
predicate
- the predicate that must be true- Returns:
- the amount of elements matching the predicate
- Throws:
java.lang.NullPointerException
- in casepredicate
is null
-
filter
Collection<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 interfaceStreamable<T>
- Specified by:
filter
in interfaceTraversable<T>
- Parameters:
predicate
- the predicate to apply to the contents of this- Returns:
- the filtered value
-
first
default Optional<T> first(java.util.function.Predicate<T> predicate)
Description copied from interface:Traversable
Find the first match in the elements using the providedPredicate
. The returned Optional isnull
safe and will either contain the element or be an emptyOptional
.Example:
// the result will be an Optional with the value 2 int firstMatch = Collection(1, 2, 3, 4) .first(i -> i % 2 == 0);
- Specified by:
first
in interfaceTraversable<T>
- Parameters:
predicate
- the predicate to use- Returns:
- the first match found
-
foldLeft
default <U> U foldLeft(U start, java.util.function.BiFunction<? super U,? super T,? extends U> combiner)
Description copied from interface:Foldable
Folds the elements from the left, starting with thestart
value and combining the result by successively calling thecombiner
operation.Example:
// result "!test" List("t", "e", "s", "t").foldLeft("!", (x, y) -> x + y)
-
foldRight
default <U> U foldRight(U start, java.util.function.BiFunction<? super T,? super U,? extends U> combiner)
Description copied from interface:Foldable
Folds the elements from the right, starting with thestart
value and combining the result by successively calling thecombiner
.Example:
// result "tset!" List("t", "e", "s", "t").foldRight("!", (x, y) -> x + y)
-
get
default T get()
Fetch the head of the collection and return it. The following operations will all return the same value for a non empty collection.
The caller must verify the collection is not empty using either size() or isEmpty() to prevent aCollection(0, 1).get(); Collection(0, 1).head();
NoSuchElementException
to be thrown.
-
head
T head()
Fetch the head of the collection and return it. The following operations will all return the same value for a non empty collection.
The caller must verify the collection is not empty using either size() or isEmpty() to prevent aCollection(0, 1).get(); Collection(0, 1).head();
NoSuchElementException
to be thrown.- Returns:
- the first element in the collection
- Throws:
java.util.NoSuchElementException
- if the collection is#isEmpty
-
isEmpty
default boolean isEmpty()
Convenience method to see if the current list is empty or not. This method will yield the same result as checking if the size() returns the value 0.- Returns:
- true if the list contains elements, otherwise false
- See Also:
size()
-
isSingleValued
default boolean isSingleValued()
Description copied from interface:Value
Is this instance single-valued or not.- Specified by:
isSingleValued
in interfaceValue<T>
- Returns:
true
if single-valued, otherwisefalse
-
last
default Optional<T> last(java.util.function.Predicate<T> predicate)
Description copied from interface:Traversable
Find the last match in the Iterator using the providedPredicate
. The returned Optional isnull
safe and will either contain the element or be an emptyOptional
.Example:
// the result will be an Optional with the value 4 int firstMatch = Collection(1, 2, 3, 4) .last(i -> i % 2 == 0);
- Specified by:
last
in interfaceTraversable<T>
- Parameters:
predicate
- the predicate to use- Returns:
- the last match found
-
map
<U> Collection<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 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
-
orElse
Collection<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 interfaceTraversable<T>
- Parameters:
other
- the alternative- Returns:
- this
Traversable
if non empty, orother
-
orElse
Collection<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 interfaceTraversable<T>
- Parameters:
supplier
- the supplier to generate the other- Returns:
- this (@code Traversable} if non empty, otherwise other
-
reduceLeft
default T reduceLeft(java.util.function.BiFunction<? super T,? super T,? extends T> reducer)
Description copied from interface:Foldable
Reduce the collection from the left, starting with the first element in the collection and successively calling the
reducer
until there are no more elements.Example:
// result "test" List("t", "e", "s", "t").reduceLeft((x, y) -> x + y)
This operation is similar to the Foldable.foldLeft(Object, BiFunction) where the first element is the head of the elements.
- Specified by:
reduceLeft
in interfaceFoldable<T>
- Parameters:
reducer
- the operation to use to reduce the elements to one value- Returns:
- the reduced value
- See Also:
Foldable.foldLeft(Object, BiFunction)
-
reject
Collection<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 interfaceTraversable<T>
- Parameters:
predicate
- the predicate to use- Returns:
- the elements that do not match the
predicate
-
split
Pair<? extends Collection<T>,? extends Collection<T>> split(java.util.function.Predicate<T> predicate)
The split operation is an execution that combines thereject(Predicate)
and thefilter(Predicate)
methods into one. Separating the values into 2 separate buckets. The first bucket will contain the same as thefilter(Predicate)
operation. The second bucket will contain the result of thereject(Predicate)
operation.- Parameters:
predicate
- the predicate to use- Returns:
- the two buckets of this operation
-
size
int size()
Get the amount of elements contained in the collection.- Returns:
- 0 if empty, otherwise the amount of entries
-
summing
default long summing(java.util.function.BiFunction<java.lang.Long,T,java.lang.Long> accumulator)
Sum all elements using the provided accumulator. This reduces the Collection to a single numbered value.- Parameters:
accumulator
- the accumulator to be used- Returns:
- the resulting value
- Throws:
java.lang.NullPointerException
- in caseaccumulator
is null
-
tail
Collection<T> tail()
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.- Returns:
- a collection containing the tail
- Throws:
java.util.NoSuchElementException
- if the collection is#isEmpty
-
-