Learn how to find the first element of a collection that matches a condition in Java using Eclipse Collections.

Image for post
Image for post
Tomato <-> To.ma.to.

Detect / DetectWith

Use to find the first element of a collection that matches a given . Use to find the first element of a collection that matches a given with an extra parameter. The method works well with method references. If no element is found that matches then 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 to find the first element of a collection that matches a given . If there is no match returns the result of evaluating the given . The method is similar to , 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 is the equivalent of on a Java . This method will return an which can be queried to determine if any element has matched the given . The method is similar to , 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

  1. / — finds the first element of a collection that matches a given or . If there is no match, then is returned.
  2. / — finds the first element of a collection that matches a given or . If there is no match, then the result of evaluating the given is returned.
  3. / — finds the first element of a collection that matches a given or . The result of the call will be an , 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.

Eclipse Collections is open for contributions. If you like the library, you can let us know by starring it on GitHub.

Written by

Java Champion. Creator of the Eclipse Collections OSS Java library (http://www.eclipse.org/collections/). Inspired by Smalltalk. Opinions are my own.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store