README_EXAMPLES.md
March 29, 2026 ยท View on GitHub
Some Quick Code Examples
Eclipse Collections puts iteration methods directly on the container types. Here's several code examples that demonstrate the simple and flexible style of programming with Eclipse Collections.
First, we will define a record class named Person with a first and last name. We will add a lastNameEquals method to the record to be used as a Predicate in either a lambda or a method reference. Using lastNameEquals, which takes a String parameter, as a method reference would be impossible with Java Stream today. When you see the examples that follow, you will discover APIs in Eclipse Collections that make it possible to use methods with a single parameter as a method reference.
public record Person(String firstName, String lastName)
{
public boolean lastNameEquals(String name)
{
return name.equals(this.lastName);
}
}
Example 1: Collect (aka map, transform)
First we will create a MutableList with three instances of the Person class.
MutableList<Person> people = Lists.mutable.with(
new Person("Sally", "Smith"),
new Person("Ted", "Watson"),
new Person("Mary", "Williams"));
Then we will collect their last names into a new MutableList, and finally output the names to a comma delimited String using makeString.
MutableList<String> lastNames = people.collect(person -> person.lastName());
assertEquals("Smith, Watson, Williams", lastNames.makeString());
The lambda in the example above can also be replaced with a method reference.
MutableList<String> lastNames = people.collect(Person::lastName);
Eclipse Collections has support for both Mutable and Immutable collections, and the return types of methods are covariant. Here we use the same Lists factory to create an ImmutableList.
ImmutableList<Person> people = Lists.immutable.with(
new Person("Sally", "Smith"),
new Person("Ted", "Watson"),
new Person("Mary", "Williams"));
While the collect method on a MutableList returned a MutableList, the collect method on an ImmutableList will return an ImmutableList.
ImmutableList<String> lastNames = people.collect(Person::lastName);
assertEquals("Smith, Watson, Williams", lastNames.makeString());
Eclipse Collections has a lazy API as well, which is available by calling the method asLazy. The method collect will now return a LazyIterable. The LazyIterable that is returned does not evaluate anything until the call to a terminal method is made. In this case, the call to makeString will force the LazyIterable to collect the last names.
LazyIterable<String> lastNames = people.asLazy().collect(Person::lastName);
assertEquals("Smith, Watson, Williams", lastNames.makeString());
Example 2: Select / Reject (aka filter / !filter)
We can find all of the people with the last name "Smith" using the method named select.
MutableList<Person> people = Lists.mutable.with(
new Person("Sally", "Smith"),
new Person("Ted", "Watson"),
new Person("Mary", "Williams"));
MutableList<Person> smiths = people.select(person -> person.lastNameEquals("Smith"));
assertEquals("Smith", smiths.collect(Person::lastName).makeString());
If we want to use a method reference, we can use the method selectWith.
MutableList<Person> smiths = people.selectWith(Person::lastNameEquals, "Smith");
assertEquals("Smith", smiths.collect(Person::lastName).makeString());
We can find all the people who do not have a last name of "Smith" using the method named reject.
MutableList<Person> notSmiths = people.reject(person -> person.lastNameEquals("Smith"));
assertEquals("Watson, Williams", notSmiths.collect(Person::lastName).makeString());
If we want to use a method reference, we can use the method rejectWith.
MutableList<Person> notSmiths = people.rejectWith(Person::lastNameEquals, "Smith");
assertEquals("Watson, Williams", notSmiths.collect(Person::lastName).makeString());
Example 3: Any / All / None
We can test whether any, all or none of the elements of a collection satisfy a given condition.
// anySatisfy with lambda
assertTrue(people.anySatisfy(person -> person.lastNameEquals("Smith"));
// anySatisfyWith with method reference
assertTrue(people.anySatisfyWith(Person::lastNameEquals, "Smith"));
// allSatisfy with lambda
assertFalse(people.allSatisfy(person -> person.lastNameEquals("Smith"));
// allSatisfyWith with method reference
assertFalse(people.allSatisfyWith(Person::lastNameEquals, "Smith"));
// noneSatisfy with lambda
assertFalse(people.noneSatisfy(person -> person.lastNameEquals("Smith"));
// noneSatisfyWith with method reference
assertFalse(people.noneSatisfyWith(Person::lastNameEquals, "Smith"));
