EC by Example: Filtering

Learn how to filter a collection using Eclipse Collections.

Photo by Tyler Nix on Unsplash

Filtering: Include or Exclude?

If you have a single method named filter, how do you know if it is supposed to be an inclusive or exclusive filter? In Eclipse Collections, there are two filtering methods named select and reject.

Filtering an Object List

@Test
public void filteringUsingSelectAndReject()
{
MutableList<Integer> mList = mList(1, 2, 3, 4, 5);
ImmutableList<Integer> iList = iList(1, 2, 3, 4, 5);

Predicate<Integer> evens = i -> i % 2 == 0;
MutableList<Integer> evensMutable = mList.select(evens);
ImmutableList<Integer> evensImmutable = iList.select(evens);
LazyIterable<Integer> evensLazy = mList.asLazy().select(evens); ExecutorService executor = Executors.newWorkStealingPool();
ParallelListIterable<Integer> evensParallel =
mList.asParallel(executor, 2).select(evens);

ImmutableList<Integer> expectedEvens = iList(2, 4);
Assert.assertEquals(expectedEvens, evensMutable);
Assert.assertEquals(expectedEvens, evensImmutable);
Assert.assertEquals(expectedEvens, evensLazy.toList());
Assert.assertEquals(expectedEvens, evensParallel.toList());

MutableList<Integer> oddsMutable = mList.reject(evens);
ImmutableList<Integer> oddsImmutable = iList.reject(evens);
LazyIterable<Integer> oddsLazy = mList.asLazy().reject(evens); ParallelListIterable<Integer> oddsParallel =
mList.asParallel(executor, 2).reject(evens);

ImmutableList<Integer> expectedOdds = iList(1, 3, 5);
Assert.assertEquals(expectedOdds, oddsMutable);
Assert.assertEquals(expectedOdds, oddsImmutable);
Assert.assertEquals(expectedOdds, oddsLazy.toList());
Assert.assertEquals(expectedOdds, oddsParallel.toList());
}

Filtering a primitive List

@Test
public void filteringPrimitivesUsingSelectAndReject()
{
MutableIntList mList = IntLists.mutable.with(1, 2, 3, 4, 5);
ImmutableIntList iList = IntLists.immutable.with(1, 2, 3, 4, 5);
IntPredicate evens = i -> i % 2 == 0;

MutableIntList evensMutable = mList.select(evens);
ImmutableIntList evensImmutable = iList.select(evens);
LazyIntIterable evensLazy = mList.asLazy().select(evens);

MutableIntList expectedEvens = IntLists.mutable.with(2, 4);
Assert.assertEquals(expectedEvens, evensMutable);
Assert.assertEquals(expectedEvens, evensImmutable);
Assert.assertEquals(expectedEvens, evensLazy.toList());

MutableIntList oddsMutable = mList.reject(evens);
ImmutableIntList oddsImmutable = iList.reject(evens);
LazyIntIterable oddsLazy = mList.asLazy().reject(evens);

MutableIntList expectedOdds = IntLists.mutable.with(1, 3, 5);
Assert.assertEquals(expectedOdds, oddsMutable);
Assert.assertEquals(expectedOdds, oddsImmutable);
Assert.assertEquals(expectedOdds, oddsLazy.toList());
}

What other types support Select and Reject?

The Symmetric Sympathy is strong with select and reject.

Select and Reject is available across many types and concerns

Possible to filter both inclusively and exclusively in one iteration?

Yes. There is a method called partition. I will show partition in the next blog in this series.

APIs covered in the examples

  1. Select (Eager, Lazy and Parallel) — filters including elements that match a condition
  2. Reject (Eager, Lazy and Parallel) — filters excluding elements that match a condition
  3. mList — Creates a MutableList
  4. iList — Creates an ImmutableList
  5. IntLists.mutable.with — Creates a MutableIntList
  6. IntLists.immutable.with — Creates an ImmutableIntList
  7. asLazy — Returns a LazyIterable or LazyIntIterable
  8. asParallel — Returns a ParallelIterable
  9. toList — Converts the target Iterable into a MutableList

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 (http://www.eclipse.org/collections/). Inspired by Smalltalk. Opinions are my own.

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