A rose by any other name…

A Rosetta Stone for Smalltalk, Eclipse Collections and Java 8 Streams

Select / Filter

If you want to include items of a collection that satisfy a given condition (Predicate) you use select (EC) or filter (Streams). Eclipse Collections also has With versions of the iteration patterns that allow for more usages of method references.

// Eclipse Collections (Select)
MutableList<Person> peopleWithCats =
this.people.select(person -> person.hasPet(PetType.CAT));
// Eclipse Collections (SelectWith)
MutableList<Person> peopleWithCats =
this.people.selectWith(Person::hasPet, PetType.CAT);
// Streams (Filter)
List<Person> peopleWithCats =
this.people.stream()
.filter(person -> person.hasPet(PetType.CAT))
.collect(Collectors.toList());

Reject / Filter (!)

If you want to exclude items of a collection that satisfy a given condition (Predicate) you use reject (EC) or filter (Streams) with a negation of the predicate.

// Eclipse Collections (Reject)
MutableList<Person> peopleWithoutCats =
this.people.reject(person -> person.hasPet(PetType.CAT));
// Eclipse Collections (RejectWith)
MutableList<Person> peopleWithoutCats =
this.people.rejectWith(Person::hasPet, PetType.CAT);
// Streams (Filter !)
List<Person> peopleWithoutCats =
this.people.stream()
.filter(person -> !person.hasPet(PetType.CAT))
.collect(Collectors.toList());

Collect / Map

If you want to transform a collection of one type to a collection of another type, you use collect (EC) or map (Streams) with a Function. There is a method named collect on Streams as well, but it means something different. It is referred to as a mutable reduction. What it reduces to is up to you, so the return type of the Stream version of collect is defined by the Collector you give it. I wish the name of the method was different on Streams (e.g. mutableReduce). C’est la vie.

// Eclipse Collections
MutableList<String> firstNames =
this.people.collect(Person::getFirstName);
// Streams
MutableList<String> firstNames =
this.people.stream()
.map(Person::getFirstName)
.collect(Collectors2.toList());

FlatCollect / FlatMap

If you want to transform a collection of collections to a single collection of another type, you use flatCollect (EC) or flatMap (Streams) with a Function that either returns an Iterable (EC) or a Stream (Streams).

// Eclipse Collections (FlatCollect - Eager)
MutableSet<PetType> petTypes =
this.people.flatCollect(Person::getPetTypes)
.toSet();
// Eclipse Collections (FlatCollect - Lazy)
MutableSet<PetType> petTypes =
this.people.asLazy()
.flatCollect(Person::getPetTypes)
.toSet();
// Streams
MutableSet<PetType> petTypes =
this.people.stream()
.flatMap(person -> person.getPetTypes().stream())
.collect(Collectors2.toSet());

Detect / Filter + FindFirst + Get

If you want to return the first element of a collection which matches a given condition (Predicate), you use detect (EC) or filter + findFirst + get (Streams). If you want to return an optional value representing either the first item that matches the condition or null, you can use detectOptional (EC) or filter + findFirst (Streams).

// Eclipse Collections (DetectWith)
Person firstPersonWithCat =
this.people.detectWith(Person::hasPet, PetType.CAT);
// Eclipse Collection (DetectWithOptional)
Optional<Person> optionalFirstPerson =
this.people.detectWithOptional(Person::hasPet, PetType.CAT);
// Streams (Filter + FindFirst + Get)
Person firstPersonWithCat =
this.people.stream()
.filter(person -> person.hasPet(PetType.CAT))
.findFirst();
.get();
// Streams (Filter + FindFirst)
Optional<Person> personOptional =
this.people.stream()
.filter(person -> person.hasPet(PetType.CAT))
.findFirst();

InjectInto / Reduce

If you want to inject an initial value into a function that then injects the result of each application of the function into the next iteration, you can use injectInto (EC) or reduce (Streams). This is probably the hardest method to understand, but has the easiest examples. You can use this method to calculate sum, min, max, product, etc.

// Eclipse Collections (InjectInto)
int numberOfPets =
this.people.collectInt(Person::getNumberOfPets)
.injectInto(0, Integer::sum);
// Streams (Reduce)
int numberOfPets =
this.people.stream()
.mapToInt(Person::getNumberOfPets)
.reduce(0, Integer::sum);

--

--

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
Donald Raab

Donald Raab

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