EC by Example: Filtering
2 min readMar 15, 2018
--
Learn how to filter a collection using Eclipse Collections.
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
.
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
Select
(Eager, Lazy and Parallel) — filters including elements that match a conditionReject
(Eager, Lazy and Parallel) — filters excluding elements that match a conditionmList
— Creates aMutableList
iList
— Creates anImmutableList
IntLists.mutable.with
— Creates aMutableIntList
IntLists.immutable.with
— Creates anImmutableIntList
asLazy
— Returns aLazyIterable
orLazyIntIterable
asParallel
— Returns aParallelIterable
toList
— Converts the targetIterable
into aMutableList
Eclipse Collections is open for contributions. If you like the library, you can let us know by starring it on GitHub.