Eclipse Collections by Example: Detect
Learn how to find the first element of a collection that matches a condition in Java using Eclipse Collections.
Detect / DetectWith
Use detect
to find the first element of a collection that matches a given Predicate
. Use detectWith
to find the first element of a collection that matches a given Predicate2
with an extra parameter. The method detectWith
works well with method references. If no element is found that matches then null
is returned.
MutableList<String> rainbow = Lists.mutable.with(
"Red", "Orange", "Yellow", "Green",
"Blue", "Indigo", "Violet");
String red = rainbow.detect(each -> each.startsWith("R"));
Assert.assertEquals("Red", red);
String red2 = rainbow.detectWith(String::startsWith, "R");
Assert.assertEquals("Red", red2);
Assert.assertNull(rainbow.detect(each -> each.startsWith("T")));
Assert.assertNull(rainbow.detectWith(String::startsWith, "T"));
DetectIfNone / DetectWithIfNone
Use detectIfNone
to find the first element of a collection that matches a given Predicate
. If there is no match returns the result of evaluating the given Function0
. The method detectWithIfNone
is similar to detectWith
, and also works well with method references.
String violetIfNone =
rainbow.detectIfNone(
each -> each.startsWith("T"),
() -> "Violet");
Assert.assertEquals("Violet", violetIfNone);
String violetIfNone2 =
rainbow.detectWithIfNone(
String::startsWith,
"T",
() -> "Violet");
Assert.assertEquals("Violet", violetIfNone2);
DetectOptional / DetectWithOptional
The method detectOptional
is the equivalent of filter().findFirst()
on a Java Stream
. This method will return an Optional
which can be queried to determine if any element has matched the given Predicate
. The method detectWithOptional
is similar to detectWith
, and will work equally well with method references.
Optional<String> violetOptional =
rainbow.detectOptional(each -> each.startsWith("T"));
Assert.assertEquals("Violet", violetOptional.orElse("Violet"));
Optional<String> violetOptional2 =
rainbow.detectWithOptional(String::startsWith, "T");
Assert.assertEquals("Violet", violetOptional2.orElse("Violet"));
APIs covered in the examples
detect
/detectWith
— finds the first element of a collection that matches a givenPredicate
orPredicate2
. If there is no match, thennull
is returned.detectIfNone
/detectWithIfNone
— finds the first element of a collection that matches a givenPredicate
orPredicate2
. If there is no match, then the result of evaluating the givenFunction0
is returned.detectOptional
/detectWithOptional
— finds the first element of a collection that matches a givenPredicate
orPredicate2
. The result of the call will be anOptional
, which can be queried to determine if there was a successful match.
Check out this presentation to learn more about the origins, design and evolution of the Eclipse Collections API.
I am a Project Lead and Committer for the Eclipse Collections OSS project at the Eclipse Foundation. Eclipse Collections is open for contributions. If you like the library, you can let us know by starring it on GitHub.