Interface Streams
-
public interface StreamsThe 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 givenIterableusing the providedPredicateand returns a collection of the same type as the input, either aListor aSet.static <T,S extends java.lang.Iterable<T>>
SfilterAll(S values, java.util.function.Predicate<? super T>... predicates)Filters elements from the givenIterableusing 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 aListcontaining 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 aSetcontaining 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 givenIterableusing 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 aListcontaining 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 aSetcontaining the matching elements.static <T> TfilterFirst(java.lang.Iterable<T> values, java.util.function.Predicate<? super T>... predicates)Filters elements from the givenIterableusing 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 givenIterableusing 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 givenIterableusing the providedPredicateand collects the result into a newSetthat 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 providedIterableusing 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 sequentialStreamfrom the givenIterable.static <T> java.util.stream.Stream<T>stream(T... values)Creates a sequentialStreamfrom the given array of values.
-
-
-
Method Detail
-
stream
@Nonnull static <T> java.util.stream.Stream<T> stream(T... values)
Creates a sequentialStreamfrom the given array of values.This method is useful for converting an array into a
Streamto 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
Streambacked by the given array
-
stream
@Nonnull static <T> java.util.stream.Stream<T> stream(java.lang.Iterable<T> iterable)
Creates a sequentialStreamfrom the givenIterable.This method is useful for converting an
Iterable(like aListor 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- theIterableto convert into a stream- Returns:
- a sequential
Streambacked by the given iterable
-
filterStream
@Nonnull 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
@Nonnull 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 providedIterableusing 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- theIterableto 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
@Nonnull 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
@Nonnull 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 givenIterableusing the provided predicate and returns aList.This method converts the input
Iterableinto 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- theIterableto 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
@Nonnull 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
SetusingSetUtils.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
@Nonnull 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 givenIterableusing the providedPredicateand collects the result into a newSetthat 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
Setcontaining only the elements that satisfy the predicate, maintaining insertion order using aLinkedHashSet
-
filter
@Nonnull static <T,S extends java.lang.Iterable<T>> S filter(S values, java.util.function.Predicate<? super T> predicate)
Filters elements from the givenIterableusing the providedPredicateand returns a collection of the same type as the input, either aListor aSet.This method intelligently handles both
ListandSettypes by:- Detecting if the input is a
SetusingSetUtils#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
@Nonnull static <T,S extends java.lang.Iterable<T>> S filterAll(S values, java.util.function.Predicate<? super T>... predicates)
Filters elements from the givenIterableusing 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
Listor 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
@Nonnull 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 aListcontaining 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
Listcontaining only the elements that satisfy all predicates
-
filterAllSet
@Nonnull 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 aSetcontaining 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
Setmaintains 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
Setcontaining only the elements that satisfy all predicates, maintaining insertion order using aLinkedHashSet
-
filterAny
@Nonnull static <T,S extends java.lang.Iterable<T>> S filterAny(S values, java.util.function.Predicate<? super T>... predicates)
Filters elements from the givenIterableusing 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
Listor 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
@Nonnull 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 aListcontaining 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
Listcontaining only the elements that satisfy at least one predicate
-
filterAnySet
@Nonnull 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 aSetcontaining 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
Setmaintains 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
Setcontaining only the elements that satisfy at least one predicate, maintaining insertion order using aLinkedHashSet
-
filterFirst
@Nullable static <T> T filterFirst(java.lang.Iterable<T> values, java.util.function.Predicate<? super T>... predicates)
Filters elements from the givenIterableusing 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
nullif 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: AliceExample 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
nullif none match
-
-