public final class PredicateUtils
extends java.lang.Object
| Modifier and Type | Method and Description |
|---|---|
static <T> java.util.function.Predicate<T> |
allCharsMatch(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.util.function.IntPredicate charPredicate)
Given a
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if all of the
characters in the character sequence returned from the function match the given IntPredicate. |
static <T,R> java.util.function.Predicate<T> |
allMatch(java.util.function.Function<? super T,? extends java.util.Collection<R>> function,
java.util.function.Predicate<R> predicate)
Given a
Function that takes an element of type <T>, and returns a
Collection<R>, this method builds a Predicate that takes an element of type
<T>, and determines if all of the elements of the collection returned by that function match a given
Predicate. |
static <T> java.util.function.Predicate<T> |
anyCharsMatch(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.util.function.IntPredicate charPredicate)
Given a
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if any of the
characters in the character sequence returned from the function match the given IntPredicate. |
static <T,R> java.util.function.Predicate<T> |
anyMatch(java.util.function.Function<? super T,? extends java.util.Collection<R>> function,
java.util.function.Predicate<R> predicate)
Given a
Function that takes an element of type <T>, and returns a
Collection<R>, this method builds a Predicate that takes an element of type
<T>, and determines if any of the elements of the collection returned by that function match a given
Predicate. |
static <T> java.util.function.Predicate<T> |
constant(boolean b)
Builds a predicate based on a passed constant
boolean value. |
static <T,R> java.util.function.Predicate<T> |
contains(java.util.Collection<? extends R> collection,
java.util.function.Function<? super T,? extends R> function)
Given a
Collection whose elements are of type <R>, and a Function that takes an
element of type <T> and returns a value of type <R>, this method builds a Predicate that
takes an element of type <T>, and determines if the given collection contains the value returned by the
function. |
static <T,R> java.util.function.Predicate<T> |
containsAll(java.util.Collection<? extends R> collection,
java.util.function.Function<? super T,java.util.Collection<? extends R>> function)
Given a
Collection whose elements are of type <R>, and a Function that takes an
element of type <T> and returns a Collection whose elements are of type <R>, this method
builds a Predicate that takes an element of type <T>, and determines if the given collection
contains all of the values in the collection returned by the function. |
static <T> java.util.function.Predicate<T> |
containsChar(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
int searchChar)
Given a
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function contains at least one character matching the given
searchChar. |
static <T> java.util.function.Predicate<T> |
containsCharIgnoreCase(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
int searchChar)
Given a
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function contains at least one character matching the given
searchChar ignoring case. |
static <T,R> java.util.function.Predicate<T> |
containsKey(java.util.Map<R,?> map,
java.util.function.Function<? super T,? extends R> function)
Given a
Map whose keys are of type <R>, and a Function that takes an element of
type <T> and returns a value of type <R>, this method builds a Predicate that takes an
element of type <T>, and determines if the given map contains a key equal to the value returned by the
function. |
static <T> java.util.function.Predicate<T> |
containsSeq(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.lang.CharSequence searchSeq)
Given a
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function contains at least one matching instance of the given
searchSeq. |
static <T> java.util.function.Predicate<T> |
containsSeqIgnoreCase(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.lang.CharSequence searchSeq)
Given a
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function contains at least one matching instance of the given
searchSeq ignoring case. |
static <T,R> java.util.function.Predicate<T> |
containsValue(java.util.Map<?,R> map,
java.util.function.Function<? super T,? extends R> function)
Given a
Map whose values are of type <R>, and a Function that takes an element of
type <T> and returns a value of type <R>, this method builds a Predicate that takes an
element of type <T>, and determines if the given map contains at least one value equal to the value
returned by the function. |
static <T> java.util.function.Predicate<T> |
distinctByKey(java.util.function.Function<? super T,?> function)
Given a
Function that takes an element of type <T>, and returns a key value of any type, this
method builds a Predicate that takes an element of type <T>, and determines whether no
elements that have been encountered so far have the same key returned from the function. |
static <T> java.util.function.Predicate<T> |
distinctByKeyParallel(java.util.function.Function<? super T,?> function)
Given a
Function that takes an element of type <T>, and returns a key value of any type, this
method builds a Predicate that takes an element of type <T>, and determines whether no
elements that have been encountered so far have the same key returned from the function. |
static <T> java.util.function.Predicate<T> |
endsWith(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.lang.CharSequence suffix)
Given a
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function ends with a given suffix. |
static <T> java.util.function.Predicate<T> |
endsWithIgnoreCase(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.lang.CharSequence suffix)
Given a
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function ends with a given suffix ignoring case. |
static <T> java.util.function.Predicate<T> |
equalsIgnoreCase(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.lang.CharSequence value)
Given a
Function that takes an element of type <T>, and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
CharSequence returned by that Function is equal to a passed CharSequence
ignoring case. |
static <T> java.util.function.Predicate<T> |
fromMapper(java.util.function.Function<T,java.lang.Boolean> function)
Allows conversion of any mapper
Function to a Predicate. |
static <T,R extends java.lang.Comparable<R>> |
gt(java.util.function.Function<? super T,? extends R> function,
R compareTo)
Given a
Function that takes an element of type <T> and returns a value of type <R>, this
method builds a Predicate that takes an element of type <T>, and determines if the value
returned from the function is greater than the compareTo value passed. |
static <T,R extends java.lang.Comparable<R>> |
gte(java.util.function.Function<? super T,? extends R> function,
R compareTo)
Given a
Function that takes an element of type <T> and returns a value of type <R>, this
method builds a Predicate that takes an element of type <T>, and determines if the value
returned from the function is greater than or equal to the compareTo value passed. |
static <T,R> java.util.function.Predicate<T> |
inverseContains(java.util.function.Function<? super T,? extends java.util.Collection<R>> function,
R value)
Given a
Function that takes an element of type <T>, and returns a
Collection<R>, this method builds a Predicate that takes an element of type
<T>, and determines if the collection returned by that function contains the passed value of
type <R>. |
static <T,R> java.util.function.Predicate<T> |
inverseContainsAll(java.util.function.Function<? super T,? extends java.util.Collection<R>> function,
java.util.Collection<? extends R> collection)
Given a
Function that takes an element of type <T>, and returns a
Collection<R>, this method builds a Predicate that takes an element of type
<T>, and determines if the collection returned by that function contains all of the value in the passed
Collection of elements of type <R>. |
static <T,U> java.util.function.Predicate<T> |
inversePredicate(java.util.function.BiPredicate<? super U,? super T> biPredicate,
U value)
As in the
predicate(BiPredicate, Object) method, builds a Predicate<T> from a passed
BiPredicate, which can be very useful in the common situation where you are streaming through a
collection of elements, and have a predicate method to call that takes two parameters. |
static <T> java.util.function.Predicate<T> |
isAlpha(java.util.function.Function<? super T,? extends java.lang.CharSequence> function)
Given a
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function is made up entirely of alpha characters. |
static <T> java.util.function.Predicate<T> |
isAlphanumeric(java.util.function.Function<? super T,? extends java.lang.CharSequence> function)
Given a
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function is made up entirely of alphanumeric characters. |
static <T> java.util.function.Predicate<T> |
isCollEmpty(java.util.function.Function<? super T,? extends java.util.Collection<?>> function)
Given a
Function that takes an element of type <T>, and returns a
Collection whose elements may be of any type, this method builds a Predicate that takes
an element of type <T>, and determines if the collection returned by that function is empty. |
static <T> java.util.function.Predicate<T> |
isCollNotEmpty(java.util.function.Function<? super T,? extends java.util.Collection<?>> function)
Given a
Function that takes an element of type <T>, and returns a
Collection whose elements may be of any type, this method builds a Predicate that takes
an element of type <T>, and determines if the collection returned by that function is not empty. |
static <T,R> java.util.function.Predicate<T> |
isEqual(java.util.function.Function<? super T,? extends R> function,
R value)
Given a
Function that takes an element of type <T>, and returns a value of type <R>,
this method builds a Predicate that takes an element of type <T>, and determines if the
value returned by that Function is equal to the passed value, according to
Object.equals(...). |
static <T,R> java.util.function.Predicate<T> |
isNotEqual(java.util.function.Function<? super T,? extends R> function,
R value)
Given a
Function that takes an element of type <T>, and returns a value of type <R>,
this method builds a Predicate that takes an element of type <T>, and determines if the
value returned by that Function is not equal to the passed value, according to
Object.equals(...). |
static <T> java.util.function.Predicate<T> |
isNull(java.util.function.Function<? super T,?> function)
Given a
Function that takes an element of type <T> and returns an object of any type, this
method builds a Predicate that takes an element of type <T>, and determines whether the
value returned from the function is null. |
static <T> java.util.function.Predicate<T> |
isNumeric(java.util.function.Function<? super T,? extends java.lang.CharSequence> function)
Given a
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function is made up entirely of alpha characters. |
static <T> java.util.function.Predicate<T> |
isSeqEmpty(java.util.function.Function<? super T,? extends java.lang.CharSequence> function)
Given a
Function that takes an element of type <T>, and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
CharSequence returned by that Function is empty. |
static <T> java.util.function.Predicate<T> |
isSeqNotEmpty(java.util.function.Function<? super T,? extends java.lang.CharSequence> function)
Given a
Function that takes an element of type <T>, and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
CharSequence returned by that Function is not empty. |
static <T,R extends java.lang.Comparable<R>> |
lt(java.util.function.Function<? super T,? extends R> function,
R compareTo)
Given a
Function that takes an element of type <T> and returns a value of type <R>, this
method builds a Predicate that takes an element of type <T>, and determines if the value
returned from the function is less than the compareTo value passed. |
static <T,R extends java.lang.Comparable<R>> |
lte(java.util.function.Function<? super T,? extends R> function,
R compareTo)
Given a
Function that takes an element of type <T> and returns a value of type <R>, this
method builds a Predicate that takes an element of type <T>, and determines if the value
returned from the function is less than or equal to the compareTo value passed. |
static <T,R> java.util.function.Predicate<T> |
mapAndFilter(java.util.function.Function<? super T,? extends R> function,
java.util.function.Predicate<? super R> predicate)
Given a
Function that takes an element of type <T> returning a value of type <R>, and a
Predicate that takes an element of type <R>, this method builds a Predicate that
takes an element of type <T>, and applies the return value of the function to the given predicate. |
static <T> java.util.function.Predicate<T> |
noCharsMatch(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.util.function.IntPredicate charPredicate)
Given a
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if none of
the characters in the character sequence returned from the function match the given IntPredicate. |
static <T,R> java.util.function.Predicate<T> |
noneMatch(java.util.function.Function<? super T,? extends java.util.Collection<R>> function,
java.util.function.Predicate<R> predicate)
Given a
Function that takes an element of type <T>, and returns a
Collection<R>, this method builds a Predicate that takes an element of type
<T>, and determines if none of the elements of the collection returned by that function match a given
Predicate. |
static <T> java.util.function.Predicate<T> |
not(java.util.function.Predicate<T> predicate)
A predicate that simply negates the passed predicate.
|
static <T> java.util.function.Predicate<T> |
notEqualsIgnoreCase(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.lang.CharSequence value)
Given a
Function that takes an element of type <T>, and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
CharSequence returned by that Function is not equal to a passed
CharSequence ignoring case. |
static <T> java.util.function.Predicate<T> |
notNull(java.util.function.Function<T,?> function)
Given a
Function that takes an element of type <T> and returns an object of any type, this
method builds a Predicate that takes an element of type <T>, and determines whether the
value returned from the function is not null. |
static <T,U> java.util.function.Predicate<T> |
predicate(java.util.function.BiPredicate<? super T,? super U> biPredicate,
U value)
Builds a
Predicate<T> from a passed BiPredicate, which can be very useful in the
common situation where you are streaming through a collection of elements, and have a predicate method to call
that takes two parameters - the first one being the element on which you are streaming, and the second being some
constant value that will be passed to all invocations. |
static <T> java.util.function.Predicate<T> |
predicate(java.util.function.Predicate<T> predicate)
Simply casts a method reference, which takes a single parameter of type <T> and returns a
boolean (or a Boolean), to a Predicate<T>. |
static <T> java.util.function.Predicate<T> |
predicateDefault(java.util.function.Predicate<? super T> predicate,
boolean defaultIfNull)
Builds a
Predicate that, if the target element is null, then the passed
boolean default value is returned. |
static <T> java.util.function.Predicate<T> |
startsWith(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.lang.CharSequence prefix)
Given a
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function starts with a given prefix. |
static <T> java.util.function.Predicate<T> |
startsWithIgnoreCase(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.lang.CharSequence prefix)
Given a
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function starts with a given prefix ignoring case. |
public static <T> java.util.function.Predicate<T> predicate(java.util.function.Predicate<T> predicate)
boolean (or a Boolean), to a Predicate<T>. This could be useful in a
situation where methods of the Predicate interface are to be called on a method reference. In the
following example, assume that the Product.isConsumerElectronicType() and
isSmartPhone() methods return boolean values:
Collection<Product> products = ...
Collection<Product> smartPhones = products.stream()
.filter(PredicateUtils.predicate(Product::isConsumerElectronic)
.and(Product::isSmartPhone))
.collect(toList());
The Predicate.and(...) method can only be called on the method reference because of the cast. Note
that the second predicate does not need to be cast, because Predicate.and(...) already takes a
Predicate<T> just like this method, and so is already doing a cast. An additional benefit of
calling this method is the null-checking performed on the target element passed to the resulting function.T - The type of the single parameter to the Predicate.predicate - A method reference to be cast to a Predicate.public static <T,U> java.util.function.Predicate<T> predicate(java.util.function.BiPredicate<? super T,? super U> biPredicate,
U value)
Predicate<T> from a passed BiPredicate, which can be very useful in the
common situation where you are streaming through a collection of elements, and have a predicate method to call
that takes two parameters - the first one being the element on which you are streaming, and the second being some
constant value that will be passed to all invocations. This would typically be called from within a chain of
method calls based on a Stream. In the following example, assume the OrderLineItem
objects passed to the getDiscountedLineItems(...) method are to be filtered based on whether a
passed Customer gets a discount on them:
private Collection<OrderLineItem> getDiscountedLineItems(Collection<OrderLineItem> lineItems, Customer customer) {
return lineItems.stream()
.filter(PredicateUtils.predicate(this::isDiscounted, customer))
.collect(toList());
}
private boolean isDiscounted(OrderLineItem lineItem, Customer customer) {
...
}
Or, with static imports:
return lineItems.stream()
.filter(predicate(this::isDiscounted, customer))
.collect(toList());
T - The target type of the first parameter to the passed biPredicate.U - The type of the constant value to be passed as the second parameter to each invocation of
biPredicate.biPredicate - A method reference (a BiPredicate) which takes two parameters - the first of type <T>,
and the second of type <U>, either of which can be any type. The method reference will
be converted by this method to a Predicate, taking a single parameter of type <T>.
Behind the scenes, this BiPredicate will be called, passing the constant value to each
invocation as the second parameter.value - A constant value, in that it will be passed to every invocation of the passed biPredicate as
the second parameter to it, and will have the same value for each of them.public static <T,U> java.util.function.Predicate<T> inversePredicate(java.util.function.BiPredicate<? super U,? super T> biPredicate,
U value)
predicate(BiPredicate, Object) method, builds a Predicate<T> from a passed
BiPredicate, which can be very useful in the common situation where you are streaming through a
collection of elements, and have a predicate method to call that takes two parameters. In the
BiPredicate passed to this method, the parameters are basically the same as in the call to
predicate(BiPredicate, Object), but in the inverse order. Here, the first parameter is a constant value
that will be passed to all invocations of the method, and the second parameter is the target element on which you
are streaming. This would typically be called from within a chain of method calls based on a Stream.
In the following example, assume the OrderLineItem objects passed to the
getDiscountedLineItems(...) method are to be filtered based on whether a passed
Customer gets a discount on them:
private Collection<OrderLineItem> getDiscountedLineItems(Collection<OrderLineItem> lineItems, Customer customer) {
return lineItems.stream()
.filter(PredicateUtils.inversePredicate(this::isDiscounted, customer))
.collect(toList());
}
private boolean isDiscounted(Customer customer, OrderLineItem lineItem) {
...
}
Or, with static imports:
return lineItems.stream()
.filter(inversePredicate(this::isDiscounted, customer))
.collect(toList());
T - The target type of the second parameter to the passed biPredicate.U - The type of the constant value to be passed as the first parameter to each invocation of
biPredicate.biPredicate - A method reference (a BiPredicate) which takes two parameters - the first of type <U>,
and the second of type <T>, either of which can be any type. The method reference will
be converted by this method to a Predicate, taking a single parameter of type <T>.
Behind the scenes, this BiPredicate will be called, passing the constant value to each
invocation as the first parameter.value - A constant value, in that it will be passed to every invocation of the passed biPredicate as
the first parameter to it, and will have the same value for each of them.public static <T> java.util.function.Predicate<T> predicateDefault(java.util.function.Predicate<? super T> predicate,
boolean defaultIfNull)
Predicate that, if the target element is null, then the passed
boolean default value is returned. Otherwise, the passed predicate is evaluated on the element. In
the following example, assume that the Product.isConsumerProduct method returns a boolean, and
that if the target element is null, then the result should default to false:
Collection<Product> products = ...
Collection<Product> consumerProducts = products.stream()
.filter(PredicateUtils.predicateDefault(Product::isConsumerProduct, false))
.collect(toList());
This is an admittedly contrived example, because why would an element in a given Collection be
null, but it may be quite valuable, since predicates can be useful in a variety of situations, not
just in streams.T - The type of the target element on which the Predicate is to be called.predicate - A method reference which takes a single parameter of type <T>, and returns a boolean
value.defaultIfNull - A default boolean value, to be returned in case the target element is null.public static <T> java.util.function.Predicate<T> constant(boolean b)
boolean value. The target element of type <T>
that is passed to the predicate is ignored, and the constant value is simply returned. This comes in handy when
combining one predicate with another using Predicate.and(...) or Predicate.or(...).
Consider the following example:
private Collection<OrderLineItem> getDiscountedLineItems(Collection<OrderLineItem> lineItems, Customer customer) {
boolean hasBlanketDiscount = customerHasBlanketDiscount(customer);
return lineItems.stream()
.filter(PredicateUtils.constant(hasBlanketDiscount)
.or(PredicateUtils.predicate(this::isDiscounted, customer)))
.collect(toList());
}
private boolean customerHasBlanketDiscount(Customer customer) {
...
}
private boolean isDiscounted(OrderLineItem lineItem, Customer customer) {
...
}
Or, with static imports:
return lineItems.stream()
.filter(constant(hasBlanketDiscount)
.or(predicate(this::isDiscounted, customer)))
.collect(toList());
T - The type of the target element to be passed to the Predicate.b - A constant boolean value that will be the result of every invocation of the returned Predicate.public static <T> java.util.function.Predicate<T> fromMapper(java.util.function.Function<T,java.lang.Boolean> function)
Function to a Predicate. For example, say you have a
Map<String, Boolean>, and you want to use it in a predicate:
Map<String, Boolean> idToDiscount = ...
Collection<Customer> customers = ...
Collection<Customer> customersWithDiscount = customers.stream
.filter(PredicateUtils.fromMapper(MapperUtils.getValue(idToDiscount, Customer::getId)))
.collect(toList());
Or, with static imports:
Collection<Customer> customersWithDiscount = customers.stream
.filter(fromMapper(getValue(idToDiscount, Customer::getId)))
.collect(toList());
T - The type of the element passed to the given function.function - A Function that takes an element of type <T>, and returns a Boolean value.public static <T> java.util.function.Predicate<T> not(java.util.function.Predicate<T> predicate)
predicate.negate(), however using this method may improve readability.T - The type of the element to be passed to the predicate built by this method.predicate - A predicate whose result is to be negated.public static <T> java.util.function.Predicate<T> isSeqEmpty(java.util.function.Function<? super T,? extends java.lang.CharSequence> function)
Function that takes an element of type <T>, and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
CharSequence returned by that Function is empty.T - The type of the element taken by the Predicate built by this method.function - A Function that takes an element and returns a CharSequence.public static <T> java.util.function.Predicate<T> isSeqNotEmpty(java.util.function.Function<? super T,? extends java.lang.CharSequence> function)
Function that takes an element of type <T>, and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
CharSequence returned by that Function is not empty.T - The type of the element taken by the Predicate built by this method.function - A Function that takes an element and returns a CharSequence.public static <T> java.util.function.Predicate<T> equalsIgnoreCase(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.lang.CharSequence value)
Function that takes an element of type <T>, and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
CharSequence returned by that Function is equal to a passed CharSequence
ignoring case.T - The type of the element taken by the Predicate built by this method.function - A Function that takes an element and returns a CharSequence.value - A CharSequence to be compared to the result of the passed Function, to determine if it is equal
ignoring case.public static <T> java.util.function.Predicate<T> notEqualsIgnoreCase(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.lang.CharSequence value)
Function that takes an element of type <T>, and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
CharSequence returned by that Function is not equal to a passed
CharSequence ignoring case.T - The type of the element taken by the Predicate built by this method.function - A Function that takes an element and returns a CharSequence.value - A CharSequence to be compared to the result of the passed Function, to determine if it is
not equal ignoring case.public static <T,R> java.util.function.Predicate<T> isEqual(java.util.function.Function<? super T,? extends R> function,
R value)
Function that takes an element of type <T>, and returns a value of type <R>,
this method builds a Predicate that takes an element of type <T>, and determines if the
value returned by that Function is equal to the passed value, according to
Object.equals(...).T - The type of the element taken by the Predicate built by this method.R - The type of the value passed, as well as the return value of the passed function.function - A Function that takes an element and returns a value to be compared with a passed value for
equality.value - A value to be compared to the result of the passed Function for equality.public static <T,R> java.util.function.Predicate<T> isNotEqual(java.util.function.Function<? super T,? extends R> function,
R value)
Function that takes an element of type <T>, and returns a value of type <R>,
this method builds a Predicate that takes an element of type <T>, and determines if the
value returned by that Function is not equal to the passed value, according to
Object.equals(...).T - The type of the element taken by the Predicate built by this method.R - The type of the value passed, as well as the return value of the passed function.function - A Function that takes an element and returns a value to be compared with a passed value for
inequality.value - A value to be compared to the result of the passed Function for inequality.public static <T,R> java.util.function.Predicate<T> contains(java.util.Collection<? extends R> collection,
java.util.function.Function<? super T,? extends R> function)
Collection whose elements are of type <R>, and a Function that takes an
element of type <T> and returns a value of type <R>, this method builds a Predicate that
takes an element of type <T>, and determines if the given collection contains the value returned by the
function. More formally, the Predicate built by this method returns true if and only if
the passed collection contains at least one element e such that
(o == null ? e == null : o.equals(e)), o being the value returned from the passed function.T - The type of the element taken by the Predicate built by this method.R - The type of elements in the passed Collection. Also, the type of the value returned by the
passed Function.collection - A Collection of elements of type <R>, to be checked for whether it contains a value
returned from a passed function.function - A Function taking an element of type <T> and returning a value of type <R> to be
checked if it is contained in a passed Collection.public static <T,R> java.util.function.Predicate<T> inverseContains(java.util.function.Function<? super T,? extends java.util.Collection<R>> function,
R value)
Function that takes an element of type <T>, and returns a
Collection<R>, this method builds a Predicate that takes an element of type
<T>, and determines if the collection returned by that function contains the passed value of
type <R>. More formally, the Predicate built by this method returns true if and
only if the returned collection contains at least one element e such that
(o == null ? e == null : o.equals(e)).
This method is similar to contains(Collection, Function), but instead of a built predicate checking
whether a passed collection contains a value returned by a function, in this method it checks whether a
collection returned by a function contains a passed value.
T - The type of the element taken by the Predicate built by this method.R - The type of elements for collection returned by a passed Function. Also, the type of the passed
value.function - A Function that takes an element and returns a Collection of elements of type <R>.value - A value of type <R> to be checked if a Collection returned by the above Function contains
it.public static <T,R> java.util.function.Predicate<T> containsAll(java.util.Collection<? extends R> collection,
java.util.function.Function<? super T,java.util.Collection<? extends R>> function)
Collection whose elements are of type <R>, and a Function that takes an
element of type <T> and returns a Collection whose elements are of type <R>, this method
builds a Predicate that takes an element of type <T>, and determines if the given collection
contains all of the values in the collection returned by the function.T - The type of the element taken by the Predicate built by this method.R - The type of elements in the passed Collection. Also, the type of the elements in the Collection
returned by the passed Function.collection - A Collection of elements of type <R>, to be checked for whether it contains all of the
values in the collection returned from a passed function.function - A Function taking an element of type <T> and returning a Collection of values of type
<R> to be checked if its values are contained in a passed Collection.java.lang.ClassCastException - If the types of one or more elements in the Collection returned by the passed
Function are incompatible with the passed Collection.java.lang.NullPointerException - If the Collection returned by the passed Function contains one or more null elements
and the passed Collection does not permit null elements (optional).public static <T,R> java.util.function.Predicate<T> inverseContainsAll(java.util.function.Function<? super T,? extends java.util.Collection<R>> function,
java.util.Collection<? extends R> collection)
Function that takes an element of type <T>, and returns a
Collection<R>, this method builds a Predicate that takes an element of type
<T>, and determines if the collection returned by that function contains all of the value in the passed
Collection of elements of type <R>.
This method is similar to containsAll(Collection, Function), but instead of a built predicate checking
whether a passed collection contains all values in a collection returned by a function, in this method it checks
whether a collection returned by a function contains all elements of a passed collection.
T - The type of the element taken by the Predicate built by this method.R - The type of elements for collection returned by a passed Function. Also, the element type of
the passed Collection.function - A Function that takes an element and returns a Collection of elements of type <R>.collection - A Collection of elements of type <R> to be checked whether a Collection returned by the
above Function contains all of its elements.java.lang.ClassCastException - If the types of one or more elements in the specified Collection are incompatible
with the Collection returned by the passed function.java.lang.NullPointerException - If the specified collection contains one or more null elements and the Collection
returned by the passed function does not permit null elements (optional).public static <T,R> java.util.function.Predicate<T> containsKey(java.util.Map<R,?> map,
java.util.function.Function<? super T,? extends R> function)
Map whose keys are of type <R>, and a Function that takes an element of
type <T> and returns a value of type <R>, this method builds a Predicate that takes an
element of type <T>, and determines if the given map contains a key equal to the value returned by the
function. More formally, the Predicate built by this method returns true if and only if
this map contains a mapping for a key k such that
(key == null ? k == null : key.equals(k)). (There can be at most one such mapping.)T - The type of the element taken by the Predicate built by this method.R - The type of keys in the passed Map. Also, the type of the value returned by the passed Function.map - A Map whose keys are of type <R>, to be checked if it contains a key equal to the value
returned by the passed function.function - A Function taking an element of type <T> and returning a value of type <R> to be
checked if it is equal to a key in a passed Map.public static <T,R> java.util.function.Predicate<T> containsValue(java.util.Map<?,R> map,
java.util.function.Function<? super T,? extends R> function)
Map whose values are of type <R>, and a Function that takes an element of
type <T> and returns a value of type <R>, this method builds a Predicate that takes an
element of type <T>, and determines if the given map contains at least one value equal to the value
returned by the function. More formally, the Predicate built by this method returns
true if and only if this map contains at least one mapping to a value v such that
(value == null ? v == null : value.equals(v)).T - The type of the element taken by the Predicate built by this method.R - The type of values in the passed Map. Also, the type of the value returned by the passed
Function.map - A Map whose values are of type <R>, to be checked if it contains at least one value equal
to the value returned by the passed function.function - A Function taking an element of type <T> and returning a value of type <R> to be
checked if it is equal to at least one value in a passed Map.public static <T> java.util.function.Predicate<T> containsChar(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
int searchChar)
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function contains at least one character matching the given
searchChar.T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning a CharSequence to be checked if it
contains at least one character matching the given searchChar.searchChar - A character to be searched in a CharSequence returned from a given function.public static <T> java.util.function.Predicate<T> containsCharIgnoreCase(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
int searchChar)
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function contains at least one character matching the given
searchChar ignoring case.T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning a CharSequence to be checked if it
contains at least one character matching the given searchChar ignoring case.searchChar - A character to be searched in a CharSequence returned from a given function ignoring case.public static <T> java.util.function.Predicate<T> containsSeq(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.lang.CharSequence searchSeq)
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function contains at least one matching instance of the given
searchSeq.T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning a CharSequence to be checked if it
contains at least one matching instance of the given searchSeq.searchSeq - A CharSequence substring to be searched in a CharSequence returned from a given function.public static <T> java.util.function.Predicate<T> containsSeqIgnoreCase(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.lang.CharSequence searchSeq)
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function contains at least one matching instance of the given
searchSeq ignoring case.T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning a CharSequence to be checked if it
contains at least one matching instance of the given searchSeq ignoring case.searchSeq - A CharSequence substring to be searched in a CharSequence returned from a given function.public static <T> java.util.function.Predicate<T> isAlpha(java.util.function.Function<? super T,? extends java.lang.CharSequence> function)
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function is made up entirely of alpha characters. More precisely, the
returned Predicate returns true if all of the letters in the character sequence return
true for Character.isLetter(ch).T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning a CharSequence to be checked if all
of its individual characters are letters.public static <T> java.util.function.Predicate<T> isAlphanumeric(java.util.function.Function<? super T,? extends java.lang.CharSequence> function)
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function is made up entirely of alphanumeric characters. More precisely, the
returned Predicate returns true if all of the letters in the character sequence return
true for Character.isLetterOrDigit(ch).T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning a CharSequence to be checked if all
of its individual characters are letters or digits.public static <T> java.util.function.Predicate<T> isNumeric(java.util.function.Function<? super T,? extends java.lang.CharSequence> function)
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function is made up entirely of alpha characters. More precisely, the
returned Predicate returns true if all of the letters in the character sequence return
true for Character.isDigit(ch).T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning a CharSequence to be checked if all
of its individual characters are digits.public static <T> java.util.function.Predicate<T> startsWith(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.lang.CharSequence prefix)
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function starts with a given prefix.T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning a CharSequence to be checked if it
starts with a given prefix.prefix - A CharSequence prefix to be determined if a sequence returned from a function starts with it.public static <T> java.util.function.Predicate<T> startsWithIgnoreCase(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.lang.CharSequence prefix)
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function starts with a given prefix ignoring case.T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning a CharSequence to be checked if it
starts with a given prefix ignoring case.prefix - A CharSequence prefix to be determined if a sequence returned from a function starts with it
ignoring case.public static <T> java.util.function.Predicate<T> endsWith(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.lang.CharSequence suffix)
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function ends with a given suffix.T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning a CharSequence to be checked if it
ends with a given suffix.suffix - A CharSequence suffix to be determined if a sequence returned from a function ends with it.public static <T> java.util.function.Predicate<T> endsWithIgnoreCase(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.lang.CharSequence suffix)
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if the
character sequence returned from the function ends with a given suffix ignoring case.T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning a CharSequence to be checked if it
ends with a given suffix ignoring case.suffix - A CharSequence suffix to be determined if a sequence returned from a function ends with it
ignoring case.public static <T> java.util.function.Predicate<T> anyCharsMatch(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.util.function.IntPredicate charPredicate)
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if any of the
characters in the character sequence returned from the function match the given IntPredicate.T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning a CharSequence to be checked
whether any of its characters match the given IntPredicate.charPredicate - An arbitrary IntPredicate to be tested to see if any characters in a sequence returned by a
given function return true for it.public static <T> java.util.function.Predicate<T> allCharsMatch(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.util.function.IntPredicate charPredicate)
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if all of the
characters in the character sequence returned from the function match the given IntPredicate.T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning a CharSequence to be checked
whether all of its characters match the given IntPredicate.charPredicate - An arbitrary IntPredicate to be tested to see if all characters in a sequence returned by a
given function return true for it.public static <T> java.util.function.Predicate<T> noCharsMatch(java.util.function.Function<? super T,? extends java.lang.CharSequence> function,
java.util.function.IntPredicate charPredicate)
Function that takes an element of type <T> and returns a CharSequence,
this method builds a Predicate that takes an element of type <T>, and determines if none of
the characters in the character sequence returned from the function match the given IntPredicate.T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning a CharSequence to be checked
whether none of its characters match the given IntPredicate.charPredicate - An arbitrary IntPredicate to be tested to see if no characters in a sequence returned by a
given function return true for it.public static <T> java.util.function.Predicate<T> isNull(java.util.function.Function<? super T,?> function)
Function that takes an element of type <T> and returns an object of any type, this
method builds a Predicate that takes an element of type <T>, and determines whether the
value returned from the function is null.T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning an object of any type to be checked
for nullity.public static <T> java.util.function.Predicate<T> notNull(java.util.function.Function<T,?> function)
Function that takes an element of type <T> and returns an object of any type, this
method builds a Predicate that takes an element of type <T>, and determines whether the
value returned from the function is not null.T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning an object of any type to be checked
for non-nullity.public static <T,R extends java.lang.Comparable<R>> java.util.function.Predicate<T> gt(java.util.function.Function<? super T,? extends R> function,
R compareTo)
Function that takes an element of type <T> and returns a value of type <R>, this
method builds a Predicate that takes an element of type <T>, and determines if the value
returned from the function is greater than the compareTo value passed.T - The type of the element taken by the Predicate built by this method.R - The type of the compareTo value passed. Also, the return type of the passed function.function - A Function taking an element of type <T> and returning a value to be checked whether it is
greater than a passed compareTo value.compareTo - A value to which a value returned from a passed function will be compared by the returned
Predicate.public static <T,R extends java.lang.Comparable<R>> java.util.function.Predicate<T> gte(java.util.function.Function<? super T,? extends R> function,
R compareTo)
Function that takes an element of type <T> and returns a value of type <R>, this
method builds a Predicate that takes an element of type <T>, and determines if the value
returned from the function is greater than or equal to the compareTo value passed.T - The type of the element taken by the Predicate built by this method.R - The type of the compareTo value passed. Also, the return type of the passed function.function - A Function taking an element of type <T> and returning a value to be checked whether it is
greater than or equal to a passed compareTo value.compareTo - A value to which a value returned from a passed function will be compared by the returned
Predicate.public static <T,R extends java.lang.Comparable<R>> java.util.function.Predicate<T> lt(java.util.function.Function<? super T,? extends R> function,
R compareTo)
Function that takes an element of type <T> and returns a value of type <R>, this
method builds a Predicate that takes an element of type <T>, and determines if the value
returned from the function is less than the compareTo value passed.T - The type of the element taken by the Predicate built by this method.R - The type of the compareTo value passed. Also, the return type of the passed function.function - A Function taking an element of type <T> and returning a value to be checked whether it is
less than a passed compareTo value.compareTo - A value to which a value returned from a passed function will be compared by the returned
Predicate.public static <T,R extends java.lang.Comparable<R>> java.util.function.Predicate<T> lte(java.util.function.Function<? super T,? extends R> function,
R compareTo)
Function that takes an element of type <T> and returns a value of type <R>, this
method builds a Predicate that takes an element of type <T>, and determines if the value
returned from the function is less than or equal to the compareTo value passed.T - The type of the element taken by the Predicate built by this method.R - The type of the compareTo value passed. Also, the return type of the passed function.function - A Function taking an element of type <T> and returning a value to be checked whether it is
less than or equal to a passed compareTo value.compareTo - A value to which a value returned from a passed function will be compared by the returned
Predicate.public static <T> java.util.function.Predicate<T> isCollEmpty(java.util.function.Function<? super T,? extends java.util.Collection<?>> function)
Function that takes an element of type <T>, and returns a
Collection whose elements may be of any type, this method builds a Predicate that takes
an element of type <T>, and determines if the collection returned by that function is empty.T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning a Collection containing elements of
any type.public static <T> java.util.function.Predicate<T> isCollNotEmpty(java.util.function.Function<? super T,? extends java.util.Collection<?>> function)
Function that takes an element of type <T>, and returns a
Collection whose elements may be of any type, this method builds a Predicate that takes
an element of type <T>, and determines if the collection returned by that function is not empty.T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning a Collection containing elements of
any type.public static <T,R> java.util.function.Predicate<T> allMatch(java.util.function.Function<? super T,? extends java.util.Collection<R>> function,
java.util.function.Predicate<R> predicate)
Function that takes an element of type <T>, and returns a
Collection<R>, this method builds a Predicate that takes an element of type
<T>, and determines if all of the elements of the collection returned by that function match a given
Predicate.T - The type of the element taken by the Predicate built by this method.R - The type of the element taken by the predicate passed. Also, the element type of the collection
returned by the passed function.function - A Function taking an element of type <T> and returning a Collection containing elements of
type <R>.predicate - A Predicate taking an element of type <R> to determine if all of the elements in a
collection returned by a given function return true for it.public static <T,R> java.util.function.Predicate<T> anyMatch(java.util.function.Function<? super T,? extends java.util.Collection<R>> function,
java.util.function.Predicate<R> predicate)
Function that takes an element of type <T>, and returns a
Collection<R>, this method builds a Predicate that takes an element of type
<T>, and determines if any of the elements of the collection returned by that function match a given
Predicate.T - The type of the element taken by the Predicate built by this method.R - The type of the element taken by the predicate passed. Also, the element type of the collection
returned by the passed function.function - A Function taking an element of type <T> and returning a Collection containing elements of
type <R>.predicate - A Predicate taking an element of type <R> to determine if any of the elements in a
collection returned by a given function return true for it.public static <T,R> java.util.function.Predicate<T> noneMatch(java.util.function.Function<? super T,? extends java.util.Collection<R>> function,
java.util.function.Predicate<R> predicate)
Function that takes an element of type <T>, and returns a
Collection<R>, this method builds a Predicate that takes an element of type
<T>, and determines if none of the elements of the collection returned by that function match a given
Predicate.T - The type of the element taken by the Predicate built by this method.R - The type of the element taken by the predicate passed. Also, the element type of the collection
returned by the passed function.function - A Function taking an element of type <T> and returning a Collection containing elements of
type <R>.predicate - A Predicate taking an element of type <R> to determine if none of the elements in a
collection returned by a given function return true for it.public static <T> java.util.function.Predicate<T> distinctByKey(java.util.function.Function<? super T,?> function)
Function that takes an element of type <T>, and returns a key value of any type, this
method builds a Predicate that takes an element of type <T>, and determines whether no
elements that have been encountered so far have the same key returned from the function. Using the predicate
built by this method in a Stream.filter(...) will ensure that two elements with the same key value
will not be present in the same stream.
Note that this violates the edict that stream operations be stateless. This means a couple of things. First, that the predicate returned by this method is not a true function, and second that the result of the predicate may depend on state which might change during the execution of the stream pipeline. This will still return correct results if you are certain that it does not matter which element having the same key ends up in the results of a terminal operation on a stream.
T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning a key value determining the
uniqueness of elements in the results of a terminal operation on a stream.public static <T> java.util.function.Predicate<T> distinctByKeyParallel(java.util.function.Function<? super T,?> function)
Function that takes an element of type <T>, and returns a key value of any type, this
method builds a Predicate that takes an element of type <T>, and determines whether no
elements that have been encountered so far have the same key returned from the function. Using the predicate
built by this method in a Stream.filter(...) will ensure that two elements with the same key value
will not be present in the same stream.
Note that this violates the edict that stream operations be stateless. This means a couple of things. First, that the predicate returned by this method is not a true function, and second that the result of the predicate may depend on state which might change during the execution of the stream pipeline. This will still return correct results if you are certain that it does not matter which element having the same key ends up in the results of a terminal operation on a stream.
The difference between this method and distinctByKey(Function) is that this method uses synchronization
to ensure correct behavior in parallel streams.
T - The type of the element taken by the Predicate built by this method.function - A Function taking an element of type <T> and returning a key value determining the
uniqueness of elements in the results of a terminal operation on a stream.public static <T,R> java.util.function.Predicate<T> mapAndFilter(java.util.function.Function<? super T,? extends R> function,
java.util.function.Predicate<? super R> predicate)
Function that takes an element of type <T> returning a value of type <R>, and a
Predicate that takes an element of type <R>, this method builds a Predicate that
takes an element of type <T>, and applies the return value of the function to the given predicate. It is a
way of adapting a given predicate to another of a different type. For example, the
StreamUtils.indexOfFirst(Collection, Predicate) method uses this predicate in its implementation (Note
that the pairWithIndex() method below refers to MapperUtils.pairWithIndex()):
public static <T> int indexOfFirst(Collection<T> objects, Predicate<T> predicate) {
return defaultStream(objects)
.map(pairWithIndex())
.filter(mapAndFilter(ObjectIndexPair::getObject, predicate))
.mapToInt(ObjectIndexPair::getIndex)
.findFirst()
.orElse(-1);
}
The map-and-filter predicate is necessary in this case because we have a predicate that operates on the original
element type of the stream, but a mapping operation has changed the type to a ObjectIndexPair. The
ObjectIndexPair::getObject method reference retrieves the original element before the predicate
evaluates it.T - The type of the element taken by the Predicate built by this method.R - The type of the element taken by the passed predicate. Also, the type of the return value of the
passed function.function - A Function to transform an element of type <T> to one of type <R> before it is
passed to a Predicate.predicate - A Predicate taking an element of type <R>, whose value will be retrieved from a given
transformer function.