Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Eclipse Collections by Example: Filtering

DZone's Guide to

Eclipse Collections by Example: Filtering

See how Eclipse Collections makes it easy to filter your lists inclusively, exclusively, or both with these examples from Java 8 and Java 10.

· Java Zone ·
Free Resource

Get the Edge with a Professional Java IDE. 30-day free trial.

Here, we'll learn how to filter a collection in Java using Eclipse Collections. Examples are shown using Java 8 and Java 10.

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 (Java 8)

@Test
public void filteringUsingSelectAndReject()
{
    ExecutorService executor = Executors.newWorkStealingPool();
    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);

    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 an Object List (Java 10)

@Test
public void filteringUsingSelectAndRejectJava10()
{
    var executor = Executors.newWorkStealingPool();

    var mutableList = mList(1, 2, 3, 4, 5);
    var immutableList = iList(1, 2, 3, 4, 5);

    Predicate<Integer> evens = i -> i % 2 == 0;

    var evensMutable = mutableList.select(evens);

    var evensImmutable = immutableList.select(evens);

    var evensLazy = mutableList.asLazy().select(evens);

    var evensParallel = mutableList.asParallel(executor, 2).select(evens);

    var expectedEvens = iList(2, 4);
    Assert.assertEquals(expectedEvens, evensMutable);
    Assert.assertEquals(expectedEvens, evensImmutable);
    Assert.assertEquals(expectedEvens, evensLazy.toList());
    Assert.assertEquals(expectedEvens, evensParallel.toList());

    var oddsMutable = mutableList.reject(evens);

    var oddsImmutable = immutableList.reject(evens);

    var oddsLazy = mutableList.asLazy().reject(evens);

    var oddsParallel = mutableList.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());
}


In the Java 10 examples, I am using the new Local-variable type inference feature with the var keyword. I recommend reading this Style Guide for Local-variable type inference from Stuart Marks. Notice how I changed the names of my variables when I removed the types and used the var keyword.

Filtering a Primitive List (Java 8)

@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());
}


Filtering a Primitive List (Java 10)

@Test
public void filteringPrimitivesUsingSelectAndRejectJava10()
{
    var mutableIntList = IntLists.mutable.with(1, 2, 3, 4, 5);
    var immutableIntList = IntLists.immutable.with(1, 2, 3, 4, 5);
    IntPredicate evens = i -> i % 2 == 0;

    var evensMutable = mutableIntList.select(evens);

    var evensImmutable = immutableIntList.select(evens);

    var evensLazy = mutableIntList.asLazy().select(evens);

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

    var oddsMutable = mutableIntList.reject(evens);

    var oddsImmutable = immutableIntList.reject(evens);

    var oddsLazy = mutableIntList.asLazy().reject(evens);

    var 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

Is it possible to filter both inclusively and exclusively in one iteration?

Yes. There is a method called partition

APIs Covered in the Examples

  1. Select (Eager, Lazy, and Parallel): filters elements that match a condition
  2. Reject (Eager, Lazy, and Parallel): filters 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

Get the Java IDE that understands code & makes developing enjoyable. Level up your code with IntelliJ IDEA. Download the free trial.

Topics:
java ,java 10 ,java 8 ,collections framework ,open source ,eclipse collections ,lists ,filtering ,tutorial

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}