EC by Example: Short-circuiting methods

Learn the methods in Eclipse Collections that short-circuit and return a result as soon as a condition is met.

Short-circuiting methods won’t leave you stuck in a loop for longer than necessary

What are the short-circuiting methods?

The short-circuiting methods in Eclipse Collections all take a or as a parameter. We refer to them as short-circuiting methods because as soon as the condition is met for a particular algorithm, the loop is broken out of and a result is immediately returned.

“the”::equals is the method reference equivalent of string -> “the”.equals(string)

The short-circuiting methods in Eclipse Collections are:

The methods that include the suffix “With” all take , while the other methods take a .

All of the examples in this blog are written using the Java 10 Local-Variable Type Inference feature (aka ).

A few forms of detect

public void detect()
var mutableList =
mList("the", "quick", "brown", "fox", "jumps");
var immutableList =
iList("over", "the", "lazy", "dog");

var the1 =
var the2 =
immutableList.detectIfNone("fox"::equals, () -> "the");

Assert.assertEquals(the1, the2);

var the3 =
mutableList.detectWith(Object::equals, "the");
var the4 =
immutableList.detectWithIfNone(Object::equals, "fox",
() -> "the");

Assert.assertEquals(the3, the4);

var the5 =
mutableList.detectOptional(s -> s.endsWith("e"));
var the6 =
immutableList.detectWithOptional(String::startsWith, "t");

Assert.assertEquals(the5.get(), the6.get());

Detecting the first and last index

public void detectIndex()
var mutableList =
mList("the", "quick", "brown", "fox", "jumps");
var immutableList =
iList("over", "the", "lazy", "dog");

var the1 = mutableList.detectIndex("the"::equals);
var the2 = immutableList.detectIndex("the"::equals);
Assert.assertEquals(0, the1);
Assert.assertEquals(1, the2);

var fox = mutableList.detectLastIndex("fox"::equals);
var dog = immutableList.detectLastIndex("dog"::equals);
Assert.assertEquals(3, fox);
Assert.assertEquals(3, dog);

Examples of Any/All/NoneSatisfy

public void anyAllNoneSatisfy()
var list = mList("the", "quick", "brown", "fox");

Assert.assertTrue(list.anySatisfy(s -> s.endsWith("e")));
Assert.assertFalse(list.anySatisfyWith(String::endsWith, "z"));
Assert.assertFalse(list.allSatisfyWith(String::endsWith, "e"));
Assert.assertTrue(list.allSatisfy(s -> !s.isEmpty()));
Assert.assertFalse(list.noneSatisfyWith(String::endsWith, "x"));

Try out short-circuiting methods in the EC Katas

Here’s a few solutions demonstrating short-circuiting methods in Exercise #2 of the Eclipse Collections Pet Kata.

Examples of anySatisfyWith, allSatisfy and detectWith from the Pet Kata

Check out this presentation to learn more about the origins, design and evolution of the Eclipse Collections API. There is also a video covering the slides that was recorded at an Eclipse Community Virtual Meet-up.

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

Java Champion. Creator of the Eclipse Collections OSS Java library ( 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