Package com.activeviam.collections
Interface ImmutableCollection<E>
- Type Parameters:
E- The type of elements in this collection.
- All Superinterfaces:
Iterable<E>
- All Known Subinterfaces:
ImmutableList<E>,ImmutableSet<E>
- All Known Implementing Classes:
ImmutableArrayList,ImmutableHashSet
Immutable collections with a functional API.
- Author:
- ActiveViam
-
Method Summary
Modifier and TypeMethodDescriptionCreate a new collection having the items of this collection plus one new item.Create a new collection having the items of this collection plus new items.default ImmutableCollection<E>addAll(ImmutableCollection<E> items) Create a new collection having the items of this collection plus items from another collection.default ImmutableCollection<E>Create a new collection having the items of this collection plus items from an array.addAll(Collection<? extends E> items) Create a new collection having the items of this collection plus items from another collection.booleanIndicate if a given item is contained in this collection.default booleandoesNotContain(E item) Indicate if a given item is not contained in this collection.default booleanTest if all the items of this collection match a given predicate.Find the first item that matches a given predicate.default intFind the number of iterations that were needed to find an item matching the given predicate.default Efirst()Give the first item of this collection.<R> ImmutableCollection<R>Flat map this collection.default voidforEach(BiFunction<E, Integer, Boolean> loop) Apply a function for each item of this collection until this function return false.default voidforEachUntil(Predicate<E> loop) Apply a function for each item of this collection until this function return false.default <R> Optional<R>When all the items of this collection can produce an optional result via a method, returns the first non empty result.default booleanisEmpty()Indicate if this collection is empty.default booleanIndicate if this collection contains at least one item.default <R> Rreduce(BiFunction<R, E, R> reducer, R initialValue) Apply a reducer to this collection.intsize()Give the size of this collection.default booleanTest if one of the items of this collection matches a given predicate.E[]Copy the content of this collection in an array.default E[]toArray(IntFunction<E[]> a) Transforms this collection into an array.Create a read-only java util collection from this collection.Methods inherited from interface java.lang.Iterable
forEach, iterator, spliterator
-
Method Details
-
size
int size()Give the size of this collection.- Returns:
- The size of this collection.
-
add
Create a new collection having the items of this collection plus one new item.- Parameters:
e- The items to add.- Returns:
- The new collection.
-
add
Create a new collection having the items of this collection plus new items.- Parameters:
first- The first item to add.second- The second item to add.rest- The other items to add.- Returns:
- The new collection.
-
contains
Indicate if a given item is contained in this collection.- Parameters:
item- The item to search for.- Returns:
- If this item was found in this collection.
- See Also:
-
toCollection
Collection<E> toCollection()Create a read-only java util collection from this collection.- Returns:
- The collection.
-
flatMap
Flat map this collection.- Type Parameters:
R- The type returned by the mapper.- Parameters:
mapper- The mapping function.- Returns:
- The iterable representing the mapped elements.
- See Also:
-
first
Give the first item of this collection.- Returns:
- The first item of this collection.
- Throws:
NoSuchElementException- If this collection is empty.
-
isEmpty
default boolean isEmpty()Indicate if this collection is empty.- Returns:
- If this collection is empty.
-
isNotEmpty
default boolean isNotEmpty()Indicate if this collection contains at least one item.- Returns:
- If this collection contains any item.
-
addAll
Create a new collection having the items of this collection plus items from another collection.- Parameters:
items- The items to add.- Returns:
- The new collection.
-
addAll
Create a new collection having the items of this collection plus items from another collection.- Parameters:
items- The items to add.- Returns:
- The new collection.
-
addAll
Create a new collection having the items of this collection plus items from an array.- Parameters:
items- The items to add.- Returns:
- The new collection.
-
forEachUntil
Apply a function for each item of this collection until this function return false.- Parameters:
loop- The function to call with each item of this collection.
-
forEach
Apply a function for each item of this collection until this function return false.- Parameters:
loop- The function to call with each item of this collection and its index.
-
reduce
Apply a reducer to this collection.- Type Parameters:
R- The output type of the reducer.- Parameters:
reducer- The reducer.initialValue- The initial value.- Returns:
- The reduced value.
- See Also:
-
some
Test if one of the items of this collection matches a given predicate.- Parameters:
predicate- The predicate to use.- Returns:
- If one of the items of this collection matches the predicate.
-
every
Test if all the items of this collection match a given predicate.- Parameters:
predicate- The predicate to use.- Returns:
- If all the items of this collection match the predicate.
-
find
Find the first item that matches a given predicate.- Parameters:
predicate- The predicate with which to test this collection's items.- Returns:
- The item in an optional. This optional will be empty if no item matched.
-
findIndex
Find the number of iterations that were needed to find an item matching the given predicate.- Parameters:
predicate- The predicate with which to test this collection's items.- Returns:
- The number of iterations, or -1 if no item matched the predicate.
-
doesNotContain
Indicate if a given item is not contained in this collection.- Parameters:
item- The item to search for.- Returns:
- If this item was not found in this collection.
- See Also:
-
toArray
Copy the content of this collection in an array.- Parameters:
a- The array to attempt copy into.- Returns:
- An array containing the items of this collection. Can be different than a if a was not large enough.
- See Also:
-
toArray
Transforms this collection into an array.- Parameters:
a- The function that creates an array of the correct type given its size as input.- Returns:
- The array containing the items in this collection.
-
getFirst
When all the items of this collection can produce an optional result via a method, returns the first non empty result.- Type Parameters:
R- The type of the value contained in the createdOptional.- Parameters:
optionalResult- The function creating the optional result from an item in the collection. Will only be called while the current result is an empty optional.- Returns:
- The optional with the first non empty result, or an empty optional if all results were empty.
-