Package me.croabeast.common
Class CollectionBuilder<T>
java.lang.Object
me.croabeast.common.CollectionBuilder<T>
- Type Parameters:
T- the type of elements in this collection builder
- All Implemented Interfaces:
Iterable<T>,Copyable<CollectionBuilder<T>>
public final class CollectionBuilder<T>
extends Object
implements Iterable<T>, Copyable<CollectionBuilder<T>>
A versatile builder and wrapper for collections, providing a fluent API for filtering,
mapping, sorting, and transforming collections.
CollectionBuilder encapsulates a mutable list of elements and allows chaining operations
such as filtering, adding, removing, mapping, and more. This utility simplifies working with collections
in a functional style, making code more readable and expressive.
Example usage:
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> evenNumbers = CollectionBuilder.of(numbers)
.filter(n -> n % 2 == 0)
.toList();
// evenNumbers now contains [2, 4]
String joined = CollectionBuilder.of("a", "b", "c")
.apply(s -> s.toUpperCase())
.toList()
.toString();
// joined is "[A, B, C]"
-
Method Summary
Modifier and TypeMethodDescriptionAdds a single element to the collection.Adds all elements from the provided iterable to this builder.addAll(Collection<? extends T> elements) Adds all elements from the provided collection to this builder.addAll(Enumeration<? extends T> elements) Adds all elements from the provided enumeration to this builder.Adds all elements from the provided iterator to this builder.final CollectionBuilder<T> Adds all provided elements to this builder.apply(UnaryOperator<T> operator) Applies a transformation function to each element in this builder.<C extends Collection<T>>
CCollects the elements of this builder into a new collection provided by the supplier.@NotNull CollectionBuilder<T> copy()Creates a copy of thisCollectionBuilder.Filters the elements of the collection based on the provided predicate.Finds and returns the first element matching the provided predicate.Finds and returns the first element matching the provided predicate.iterator()Returns an iterator over the elements in this builder.<U> CollectionBuilder<U> Maps each element in this builder to a new value using the provided function.static <T> CollectionBuilder<T> Creates a newCollectionBuilderfrom the elements provided by an iterable.static <T> CollectionBuilder<T> of(Collection<T> collection) Creates a newCollectionBuilderfrom the provided collection.static <T> CollectionBuilder<T> of(Enumeration<T> enumeration) Creates a newCollectionBuilderfrom the elements provided by an enumeration.static <T> CollectionBuilder<T> Creates a newCollectionBuilderfrom the elements provided by an iterator.static <K,V> CollectionBuilder <Map.Entry<K, V>> Creates a newCollectionBuilderfrom the entries of the provided map.static <T> CollectionBuilder<T> of(CollectionBuilder<T> builder) Creates a copy of the givenCollectionBuilder.static <T> CollectionBuilder<T> of(T... elements) Creates a newCollectionBuilderfrom an array of elements.Removes a single element from the collection.removeAll(Collection<? extends T> elements) Removes all elements in the specified collection from this builder.intsize()Returns the number of elements currently held in this builder.intsizeByFilter(Predicate<T> predicate) Returns the number of elements that satisfy a given predicate.sort(Comparator<? super T> comparator) Sorts the elements in this builder using the provided comparator.Returns anEnumerationover the elements in this builder.toList()Returns a new list containing all elements in this builder.toQueue()Returns a new queue containing all elements in this builder.toSet()Returns a new set containing all unique elements in this builder.toString()Returns a string representation of the underlying collection.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Method Details
-
iterator
Returns an iterator over the elements in this builder. -
filter
Filters the elements of the collection based on the provided predicate.Elements that do not satisfy the predicate are removed.
- Parameters:
predicate- the condition to apply for filtering- Returns:
- this builder instance for method chaining
-
add
Adds a single element to the collection.- Parameters:
element- the element to add- Returns:
- this builder instance for method chaining
-
remove
Removes a single element from the collection.- Parameters:
element- the element to remove- Returns:
- this builder instance for method chaining
-
addAll
Adds all elements from the provided collection to this builder.- Parameters:
elements- the collection of elements to add- Returns:
- this builder instance for method chaining
-
addAll
Adds all provided elements to this builder.- Parameters:
elements- an array of elements to add- Returns:
- this builder instance for method chaining
-
addAll
Adds all elements from the provided iterable to this builder.- Parameters:
elements- the iterable of elements to add- Returns:
- this builder instance for method chaining
-
addAll
Adds all elements from the provided iterator to this builder.- Parameters:
elements- the iterator of elements to add- Returns:
- this builder instance for method chaining
-
addAll
Adds all elements from the provided enumeration to this builder.- Parameters:
elements- the enumeration of elements to add- Returns:
- this builder instance for method chaining
-
removeAll
Removes all elements in the specified collection from this builder.- Parameters:
elements- the collection of elements to remove- Returns:
- this builder instance for method chaining
-
sort
Sorts the elements in this builder using the provided comparator.- Parameters:
comparator- the comparator to determine the order of the elements- Returns:
- this builder instance for method chaining
-
apply
Applies a transformation function to each element in this builder.Each element is replaced with the result of applying the operator.
- Parameters:
operator- the transformation function- Returns:
- this builder instance for method chaining
-
map
Maps each element in this builder to a new value using the provided function.- Type Parameters:
U- the type of the mapped elements- Parameters:
function- the mapping function- Returns:
- a new
CollectionBuildercontaining the mapped elements
-
copy
Creates a copy of thisCollectionBuilder. -
findFirst
Finds and returns the first element matching the provided predicate.If no element matches, returns the specified default value.
- Parameters:
predicate- the condition to testdef- the default value to return if no match is found- Returns:
- the first matching element, or the default value if none is found
-
findFirst
Finds and returns the first element matching the provided predicate.If no element matches, returns
null.- Parameters:
predicate- the condition to test- Returns:
- the first matching element, or
nullif none is found
-
collect
Collects the elements of this builder into a new collection provided by the supplier.- Type Parameters:
C- the type of the collection to return- Parameters:
supplier- a supplier to create a new collection- Returns:
- a new collection containing all elements in this builder
-
sizeByFilter
Returns the number of elements that satisfy a given predicate.- Parameters:
predicate- the condition to test- Returns:
- the count of elements that match the predicate
-
toList
Returns a new list containing all elements in this builder.- Returns:
- a
Listof elements
-
toSet
Returns a new set containing all unique elements in this builder.- Returns:
- a
Setof elements
-
toQueue
Returns a new queue containing all elements in this builder.- Returns:
- a
Queueof elements
-
toEnumeration
Returns anEnumerationover the elements in this builder.- Returns:
- an enumeration of elements
-
size
public int size()Returns the number of elements currently held in this builder.- Returns:
- the size of the collection
-
toString
Returns a string representation of the underlying collection. -
of
Creates a newCollectionBuilderfrom the provided collection.- Type Parameters:
T- the type of elements in the collection- Parameters:
collection- the source collection; ifnull, an empty collection is used- Returns:
- a new
CollectionBuilderinstance
-
of
Creates a newCollectionBuilderfrom the entries of the provided map.- Type Parameters:
K- the type of the keys in the mapV- the type of the values in the map- Parameters:
map- the source map whose entries are to be used- Returns:
- a new
CollectionBuildercontaining the map's entries
-
of
Creates a newCollectionBuilderfrom the elements provided by an iterator.- Type Parameters:
T- the type of elements- Parameters:
iterator- the source iterator of elements- Returns:
- a new
CollectionBuildercontaining the elements from the iterator
-
of
Creates a newCollectionBuilderfrom the elements provided by an iterable.- Type Parameters:
T- the type of elements- Parameters:
iterable- the source iterable of elements- Returns:
- a new
CollectionBuildercontaining the elements from the iterable
-
of
Creates a newCollectionBuilderfrom the elements provided by an enumeration.- Type Parameters:
T- the type of elements- Parameters:
enumeration- the source enumeration of elements- Returns:
- a new
CollectionBuildercontaining the elements from the enumeration
-
of
Creates a newCollectionBuilderfrom an array of elements.- Type Parameters:
T- the type of elements- Parameters:
elements- the array of elements to include- Returns:
- a new
CollectionBuildercontaining the given elements
-
of
Creates a copy of the givenCollectionBuilder.- Type Parameters:
T- the type of elements- Parameters:
builder- the builder to copy- Returns:
- a new
CollectionBuilderwith the same elements as the provided builder
-