Eclipse Collections by Example: Partitioning
Learn how to partition
a collection using Eclipse Collections.
What is partitioning?
Partitioning is a kind of filtering, except that all elements of a collection are retained. Instead of being included (like select
) or excluded (like reject
), the elements of the collection are split into two collections based on whether they return true
or false
when passed to a Predicate
.
Partitioning a List
@Test
public void partitioningLists()
{
MutableList<Integer> mList = Lists.mutable.with(1, 2, 3, 4, 5);
ImmutableList<Integer> iList = Lists.immutable.with(1, 2, 3, 4, 5);
Predicate<Integer> evens = i -> i % 2 == 0;
PartitionMutableList<Integer> mutable =
mList.partition(evens);
PartitionImmutableList<Integer> immutable =
iList.partition(evens);
PartitionIterable<Integer> lazy =
mList.asLazy().partition(evens);
ImmutableList<Integer> expectedEvens = iList(2, 4);
Assert.assertEquals(expectedEvens, mutable.getSelected());
Assert.assertEquals(expectedEvens, immutable.getSelected());
Assert.assertEquals(expectedEvens, lazy.getSelected().toList());
ImmutableList<Integer> expectedOdds = iList(1, 3, 5);
Assert.assertEquals(expectedOdds, mutable.getRejected());
Assert.assertEquals(expectedOdds, immutable.getRejected());
Assert.assertEquals(expectedOdds, lazy.getRejected().toList());
}
Covariance at play
The return type for partition is determined by the source type. In the case of a MutableList
as seen above, the method partition will return a PartitionMutableList
. The following is a partial hierarchy of types that exist for partitioning a List
. The full hierarchy includes similar relationships for Bag
, Set
, SortedSet
, SortedBag
and Stack
.
Partitioning a Set
@Test
public void partitioningSets()
{
MutableSet<Integer> mSet = Sets.mutable.with(1, 2, 3, 4, 5);
ImmutableSet<Integer> iSet = Sets.immutable.with(1, 2, 3, 4, 5);
Predicate<Integer> evens = i -> i % 2 == 0;
PartitionMutableSet<Integer> mutable =
mSet.partition(evens);
PartitionImmutableSet<Integer> immutable =
iSet.partition(evens);
PartitionIterable<Integer> lazy =
mSet.asLazy().partition(evens);
ImmutableSet<Integer> expectedEvens = iSet(2, 4);
Assert.assertEquals(expectedEvens, mutable.getSelected());
Assert.assertEquals(expectedEvens, immutable.getSelected());
Assert.assertEquals(expectedEvens, lazy.getSelected().toSet());
ImmutableSet<Integer> expectedOdds = iSet(1, 3, 5);
Assert.assertEquals(expectedOdds, mutable.getRejected());
Assert.assertEquals(expectedOdds, immutable.getRejected());
Assert.assertEquals(expectedOdds, lazy.getRejected().toSet());
}
APIs and features covered in the examples
Partition
(Eager and Lazy) — filters selecting and rejecting elements that based on a given condition. Partition is a terminal operation onLazyIterable
, which forces execution to happen.mList
— Creates aMutableList
iList
— Creates anImmutableList
mSet
— Creates aMutableSet
iSet
— Creates anImmutableSet
asLazy
— Returns aLazyIterable
orLazyIntIterable
toList
— Converts the targetIterable
into aMutableList
toSet
— Converts the targetIterable
into aMutableSet
var
— Local variable Type Inference included in Java 10 (JEP 286)
Eclipse Collections is open for contributions. If you like the library, you can let us know by starring it on GitHub.