Interface Streams
-
public interface Streams
The utilities class forStream
- Since:
- 1.0.0
- Author:
- Mercy
-
-
Method Summary
Static Methods Modifier and Type Method Description static <T,S extends java.lang.Iterable<T>>
Sfilter(S values, java.util.function.Predicate<? super T> predicate)
Filters elements from the givenIterable
using the providedPredicate
and returns a collection of the same type as the input, either aList
or aSet
.static <T,S extends java.lang.Iterable<T>>
SfilterAll(S values, java.util.function.Predicate<? super T>... predicates)
Filters elements from the givenIterable
using the combined result of multiple predicates in a logical AND fashion.static <T> java.util.List<T>
filterAllList(T[] values, java.util.function.Predicate<? super T>... predicates)
Filters elements from the given array using the combined result of multiple predicates in a logical AND fashion, and returns aList
containing the matching elements.static <T> java.util.Set<T>
filterAllSet(T[] values, java.util.function.Predicate<? super T>... predicates)
Filters elements from the given array using the combined result of multiple predicates in a logical AND fashion, and returns aSet
containing the matching elements.static <T,S extends java.lang.Iterable<T>>
SfilterAny(S values, java.util.function.Predicate<? super T>... predicates)
Filters elements from the givenIterable
using the combined result of multiple predicates in a logical OR fashion.static <T> java.util.List<T>
filterAnyList(T[] values, java.util.function.Predicate<? super T>... predicates)
Filters elements from the given array using the combined result of multiple predicates in a logical OR fashion, and returns aList
containing the matching elements.static <T> java.util.Set<T>
filterAnySet(T[] values, java.util.function.Predicate<? super T>... predicates)
Filters elements from the given array using the combined result of multiple predicates in a logical OR fashion, and returns aSet
containing the matching elements.static <T> T
filterFirst(java.lang.Iterable<T> values, java.util.function.Predicate<? super T>... predicates)
Filters elements from the givenIterable
using the combined result of multiple predicates in a logical AND fashion, and returns the first matching element.static <T,S extends java.lang.Iterable<T>>
java.util.List<T>filterList(S values, java.util.function.Predicate<? super T> predicate)
Filters the elements from the givenIterable
using the provided predicate and returns aList
.static <T> java.util.List<T>
filterList(T[] values, java.util.function.Predicate<? super T> predicate)
Filters the elements from the given array using the provided predicate and returns aList
.static <T,S extends java.lang.Iterable<T>>
java.util.Set<T>filterSet(S values, java.util.function.Predicate<? super T> predicate)
Filters elements from the givenIterable
using the providedPredicate
and collects the result into a newSet
that maintains insertion order.static <T> java.util.Set<T>
filterSet(T[] values, java.util.function.Predicate<? super T> predicate)
Filters the elements from the given array using the provided predicate and returns aSet
.static <T,S extends java.lang.Iterable<T>>
java.util.stream.Stream<T>filterStream(S values, java.util.function.Predicate<? super T> predicate)
Filters the elements from the providedIterable
using the given predicate and returns aStream
.static <T> java.util.stream.Stream<T>
filterStream(T[] values, java.util.function.Predicate<? super T> predicate)
Filters the elements from the provided array using the given predicate and returns aStream
.static <T> java.util.stream.Stream<T>
stream(java.lang.Iterable<T> iterable)
Creates a sequentialStream
from the givenIterable
.static <T> java.util.stream.Stream<T>
stream(T... values)
Creates a sequentialStream
from the given array of values.
-
-
-
Method Detail
-
stream
static <T> java.util.stream.Stream<T> stream(T... values)
Creates a sequentialStream
from the given array of values.This method is useful for converting an array into a
Stream
to enable functional-style operations such as filtering, mapping, and collecting.Example Usage
String[] names = {"Alice", "Bob", "Charlie"}; Stream<String> stream = Streams.stream(names); stream.filter(name -> name.startsWith("A")) .forEach(System.out::println); // Prints: Alice
- Type Parameters:
T
- the type of the stream elements- Parameters:
values
- the array of elements to form the stream- Returns:
- a sequential
Stream
backed by the given array
-
stream
static <T> java.util.stream.Stream<T> stream(java.lang.Iterable<T> iterable)
Creates a sequentialStream
from the givenIterable
.This method is useful for converting an
Iterable
(like aList
or aSet
) into aStream
, enabling functional-style operations such as filtering, mapping, and collecting.Example Usage
List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); Stream<String> stream = Streams.stream(names); stream.filter(name -> name.startsWith("B")) .forEach(System.out::println); // Prints: Bob
- Type Parameters:
T
- the type of the stream elements- Parameters:
iterable
- theIterable
to convert into a stream- Returns:
- a sequential
Stream
backed by the given iterable
-
filterStream
static <T> java.util.stream.Stream<T> filterStream(T[] values, java.util.function.Predicate<? super T> predicate)
Filters the elements from the provided array using the given predicate and returns aStream
.This method creates a sequential stream from the input array, then applies the given predicate to retain only the elements that match the condition.
Example Usage
String[] names = {"Alice", "Bob", "Charlie"}; Stream<String> filteredStream = Streams.filterStream(names, name -> name.startsWith("C")); filteredStream.forEach(System.out::println); // Prints: Charlie
- Type Parameters:
T
- the type of the stream elements- Parameters:
values
- the array of elements to be filteredpredicate
- the condition to apply to each element- Returns:
- a filtered stream containing only the elements that satisfy the predicate
-
filterStream
static <T,S extends java.lang.Iterable<T>> java.util.stream.Stream<T> filterStream(S values, java.util.function.Predicate<? super T> predicate)
Filters the elements from the providedIterable
using the given predicate and returns aStream
.This method creates a sequential stream from the input
Iterable
, then applies the given predicate to retain only the elements that match the condition.Example Usage
List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); Stream<String> filteredStream = Streams.filterStream(names, name -> name.startsWith("B")); filteredStream.forEach(System.out::println); // Prints: Bob
- Type Parameters:
T
- the type of the stream elements- Parameters:
values
- theIterable
to convert into a stream and apply filteringpredicate
- the condition to apply to each element- Returns:
- a filtered stream containing only the elements that satisfy the predicate
-
filterList
static <T> java.util.List<T> filterList(T[] values, java.util.function.Predicate<? super T> predicate)
Filters the elements from the given array using the provided predicate and returns aList
.This method converts the input array into a
List
, then applies the given predicate to retain only the elements that match the condition.Example Usage
String[] names = {"Alice", "Bob", "Charlie"}; List<String> filteredList = Streams.filterList(names, name -> name.startsWith("A")); System.out.println(filteredList); // Output: [Alice]
- Type Parameters:
T
- the type of the elements in the array and list- Parameters:
values
- the array of elements to be filteredpredicate
- the condition to apply to each element- Returns:
- a list containing only the elements that satisfy the predicate
-
filterList
static <T,S extends java.lang.Iterable<T>> java.util.List<T> filterList(S values, java.util.function.Predicate<? super T> predicate)
Filters the elements from the givenIterable
using the provided predicate and returns aList
.This method converts the input
Iterable
into aStream
, applies the given predicate to retain only the elements that match the condition, and then collects the result into a newList
.Example Usage
List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); List<String> filteredList = Streams.filterList(names, name -> name.startsWith("B")); System.out.println(filteredList); // Output: [Bob]
- Type Parameters:
T
- the type of the elements in the iterable and list- Parameters:
values
- theIterable
to convert into a stream and apply filteringpredicate
- the condition to apply to each element- Returns:
- a list containing only the elements that satisfy the predicate
-
filterSet
static <T> java.util.Set<T> filterSet(T[] values, java.util.function.Predicate<? super T> predicate)
Filters the elements from the given array using the provided predicate and returns aSet
.This method converts the input array into a new
Set
usingSetUtils.ofSet(Object[])
, then applies the given predicate to retain only the elements that match the condition.Example Usage
String[] names = {"Alice", "Bob", "Charlie"}; Set<String> filteredSet = Streams.filterSet(names, name -> name.startsWith("A")); System.out.println(filteredSet); // Output: [Alice]
- Type Parameters:
T
- the type of the elements in the array and set- Parameters:
values
- the array of elements to be filteredpredicate
- the condition to apply to each element- Returns:
- a set containing only the elements that satisfy the predicate
-
filterSet
static <T,S extends java.lang.Iterable<T>> java.util.Set<T> filterSet(S values, java.util.function.Predicate<? super T> predicate)
Filters elements from the givenIterable
using the providedPredicate
and collects the result into a newSet
that maintains insertion order.This method is particularly useful when you need to:
- Filter elements based on a condition
- Maintain the insertion order of filtered results
- Avoid duplicate elements during filtering
Example Usage
List<String> names = Arrays.asList("Alice", "Bob", "Charlie", "Anna"); Set<String> filteredSet = Streams.filterSet(names, name -> name.startsWith("A")); System.out.println(filteredSet); // Output: [Alice, Anna]
- Type Parameters:
T
- the type of the stream elements- Parameters:
values
- the iterable containing elements to be filteredpredicate
- the condition used to include elements in the resulting set- Returns:
- a new
Set
containing only the elements that satisfy the predicate, maintaining insertion order using aLinkedHashSet
-
filter
static <T,S extends java.lang.Iterable<T>> S filter(S values, java.util.function.Predicate<? super T> predicate)
Filters elements from the givenIterable
using the providedPredicate
and returns a collection of the same type as the input, either aList
or aSet
.This method intelligently handles both
List
andSet
types by:- Detecting if the input is a
Set
usingSetUtils.isSet(Iterable)
- Using
filterSet(Iterable, Predicate)
for sets to maintain uniqueness and order - Using
filterList(Iterable, Predicate)
for lists to preserve element order and duplicates
Example Usage with List
List<String> names = Arrays.asList("Alice", "Bob", "Charlie"); List<String> filteredList = Streams.filter(names, name -> name.startsWith("B")); System.out.println(filteredList); // Output: [Bob]
Example Usage with Set
Set<String> names = new LinkedHashSet<>(Arrays.asList("Alice", "Bob", "Charlie", "Anna")); Set<String> filteredSet = Streams.filter(names, name -> name.startsWith("A")); System.out.println(filteredSet); // Output: [Alice, Anna]
- Type Parameters:
T
- the type of the stream elementsS
- the type of the iterable, either a list or set- Parameters:
values
- the iterable containing elements to be filteredpredicate
- the condition used to include elements in the resulting collection- Returns:
- a new collection of the same type as the input, containing only the elements that satisfy the predicate
- Detecting if the input is a
-
filterAll
static <T,S extends java.lang.Iterable<T>> S filterAll(S values, java.util.function.Predicate<? super T>... predicates)
Filters elements from the givenIterable
using the combined result of multiple predicates in a logical AND fashion.This method applies all provided predicates as a single condition where an element must satisfy all predicates to be included in the resulting collection. The return type matches the input's collection type (either a
List
or aSet
) based on the same logic used byfilter(Iterable, Predicate)
.Example Usage with List
List<String> names = Arrays.asList("Alice", "Anna", "Bob", "Andrew"); List<String> filtered = Streams.filterAll(names, name -> name.startsWith("A"), name -> name.length() > 4 ); System.out.println(filtered); // Output: [Alice, Andrew]
Example Usage with Set
Set<String> names = new LinkedHashSet<>(Arrays.asList("Alice", "Anna", "Bob", "Andrew")); Set<String> filtered = Streams.filterAll(names, name -> name.startsWith("A"), name -> name.length() > 4 ); System.out.println(filtered); // Output: [Alice, Andrew]
- Type Parameters:
T
- the type of the stream elementsS
- the type of the iterable, either a list or set- Parameters:
values
- the iterable containing elements to be filteredpredicates
- the array of predicates to combine and apply to each element- Returns:
- a new collection of the same type as the input, containing only the elements that satisfy all predicates
-
filterAllList
static <T> java.util.List<T> filterAllList(T[] values, java.util.function.Predicate<? super T>... predicates)
Filters elements from the given array using the combined result of multiple predicates in a logical AND fashion, and returns aList
containing the matching elements.This method applies all provided predicates as a single condition where an element must satisfy all predicates to be included in the resulting list.
Example Usage
String[] names = {"Alice", "Anna", "Bob", "Andrew"}; List<String> filtered = Streams.filterAllList(names, name -> name.startsWith("A"), name -> name.length() > 4 ); System.out.println(filtered); // Output: [Alice, Andrew]
- Type Parameters:
T
- the type of the stream elements- Parameters:
values
- the array containing elements to be filteredpredicates
- the array of predicates to combine and apply to each element- Returns:
- a new
List
containing only the elements that satisfy all predicates
-
filterAllSet
static <T> java.util.Set<T> filterAllSet(T[] values, java.util.function.Predicate<? super T>... predicates)
Filters elements from the given array using the combined result of multiple predicates in a logical AND fashion, and returns aSet
containing the matching elements.This method applies all provided predicates as a single condition where an element must satisfy all predicates to be included in the resulting set. The returned
Set
maintains insertion order using aLinkedHashSet
.Example Usage
String[] names = {"Alice", "Anna", "Bob", "Andrew"}; Set<String> filtered = Streams.filterAllSet(names, name -> name.startsWith("A"), name -> name.length() > 4 ); System.out.println(filtered); // Output: [Alice, Andrew]
- Type Parameters:
T
- the type of the stream elements- Parameters:
values
- the array containing elements to be filteredpredicates
- the array of predicates to combine and apply to each element- Returns:
- a new
Set
containing only the elements that satisfy all predicates, maintaining insertion order using aLinkedHashSet
-
filterAny
static <T,S extends java.lang.Iterable<T>> S filterAny(S values, java.util.function.Predicate<? super T>... predicates)
Filters elements from the givenIterable
using the combined result of multiple predicates in a logical OR fashion.This method applies all provided predicates as a single condition where an element must satisfy at least one predicate to be included in the resulting collection. The return type matches the input's collection type (either a
List
or aSet
) based on the same logic used byfilter(Iterable, Predicate)
.Example Usage with List
List<String> names = Arrays.asList("Alice", "Anna", "Bob", "Andrew"); List<String> filtered = Streams.filterAny(names, name -> name.startsWith("B"), name -> name.length() <= 3 ); System.out.println(filtered); // Output: [Anna, Bob]
Example Usage with Set
Set<String> names = new LinkedHashSet<>(Arrays.asList("Alice", "Anna", "Bob", "Andrew")); Set<String> filtered = Streams.filterAny(names, name -> name.startsWith("B"), name -> name.length() <= 3 ); System.out.println(filtered); // Output: [Anna, Bob]
- Type Parameters:
T
- the type of the stream elementsS
- the type of the iterable, either a list or set- Parameters:
values
- the iterable containing elements to be filteredpredicates
- the array of predicates to combine and apply to each element- Returns:
- a new collection of the same type as the input, containing only the elements that satisfy at least one predicate
-
filterAnyList
static <T> java.util.List<T> filterAnyList(T[] values, java.util.function.Predicate<? super T>... predicates)
Filters elements from the given array using the combined result of multiple predicates in a logical OR fashion, and returns aList
containing the matching elements.This method applies all provided predicates as a single condition where an element must satisfy at least one predicate to be included in the resulting list. The order of elements is preserved, and duplicates are allowed if they exist in the input array.
Example Usage
String[] names = {"Alice", "Anna", "Bob", "Andrew"}; List<String> filtered = Streams.filterAnyList(names, name -> name.startsWith("B"), name -> name.length() <= 3 ); System.out.println(filtered); // Output: [Anna, Bob]
- Type Parameters:
T
- the type of the stream elements- Parameters:
values
- the array containing elements to be filteredpredicates
- the array of predicates to combine and apply to each element- Returns:
- a new
List
containing only the elements that satisfy at least one predicate
-
filterAnySet
static <T> java.util.Set<T> filterAnySet(T[] values, java.util.function.Predicate<? super T>... predicates)
Filters elements from the given array using the combined result of multiple predicates in a logical OR fashion, and returns aSet
containing the matching elements.This method applies all provided predicates as a single condition where an element must satisfy at least one predicate to be included in the resulting set. The returned
Set
maintains insertion order using aLinkedHashSet
.Example Usage
String[] names = {"Alice", "Anna", "Bob", "Andrew"}; Set<String> filtered = Streams.filterAnySet(names, name -> name.startsWith("B"), name -> name.length() <= 3 ); System.out.println(filtered); // Output: [Anna, Bob]
- Type Parameters:
T
- the type of the stream elements- Parameters:
values
- the array containing elements to be filteredpredicates
- the array of predicates to combine and apply to each element- Returns:
- a new
Set
containing only the elements that satisfy at least one predicate, maintaining insertion order using aLinkedHashSet
-
filterFirst
static <T> T filterFirst(java.lang.Iterable<T> values, java.util.function.Predicate<? super T>... predicates)
Filters elements from the givenIterable
using the combined result of multiple predicates in a logical AND fashion, and returns the first matching element.This method applies all provided predicates as a single condition where an element must satisfy all predicates to be considered a match. It returns the first such element found, or
null
if no element matches.If the input is a
List
, the method respects the list's iteration order. For aSet
, especially aLinkedHashSet
, it maintains insertion order during filtering.Example Usage with List
List<String> names = Arrays.asList("Alice", "Anna", "Bob", "Andrew"); String result = Streams.filterFirst(names, name -> name.startsWith("A"), name -> name.length() > 4 ); System.out.println(result); // Output: Alice
Example Usage with Set
Set<String> names = new LinkedHashSet<>(Arrays.asList("Alice", "Anna", "Bob", "Andrew")); String result = Streams.filterFirst(names, name -> name.startsWith("A"), name -> name.length() > 4 ); System.out.println(result); // Output: Alice
- Type Parameters:
T
- the type of the stream elements- Parameters:
values
- the iterable containing elements to be filteredpredicates
- the array of predicates to combine and apply to each element- Returns:
- the first element that satisfies all predicates, or
null
if none match
-
-