Eclipse Collections is an open source Java Collections framework. In this blog I am going to demonstrate four lesser known features of the framework. I have published similar blogs in Java Advent Calendars of 2018, 2019, 2020, 2021, 2022, and 2023. Please refer to the resources at the end of the blog for more information about the framework.
primitiveStream()
: Eclipse Collections offers seamless integration with Java 8+ streams. We take it a step further and provide an easier integration with Primitive Streams. ThePrimitiveStream
interfaces (IntStream
,LongStream
, andDoubleStream
) provide efficient ways to process primitive data without boxing.@Test
public void primitiveStreams() {
IntList intList = IntLists.mutable.with(1, 2, 3);
Assertions.assertEquals(
6,
intList.primitiveStream().sum());
LongList longList = LongLists.mutable.with(1L, 2L, 3L);
Assertions.assertEquals(
6L,
longList.primitiveStream().sum());
DoubleList doubleList = DoubleLists.mutable.with(
1.0, 2.0, 3.0);
Assertions.assertEquals(
6.0,
doubleList.primitiveStream().sum());
}- Immutable Primitive Collections: Eclipse Collections offers primitive collections for all seven primitives:
int
,long
,float
,double
,byte
,char
, andboolean
. In addition to mutable collections, we offer immutable collections for all the primitives. The immutable collections play an especially important role during parallel processing and improve performance.@Test
public void immutablePrimitives() {
ImmutableIntList immutableIntList = IntLists
.immutable.with(1, 2, 3);
MutableIntList mutableIntList = IntLists
.mutable.with(1, 2, 3);
Assertions.assertEquals(
mutableIntList,
immutableIntList);
// No mutating APIs; below line throws a compilation error
immutableIntList.add(4);
} Collectors2
: Eclipse Collections provides an extension forjava.util.stream.Collectors
to provide Eclipse Collections functionalities as inter-op with Java streams by extending theCollector
interface. Refer to the Javadocs here for the comprehensive set of features available inCollectors2
.
The example below illustrates one of the hidden treasures covered in 2018:partition
. As you can see, the inter-op is helpful to use Eclipse Collections iteration patterns with Java streams.@Test
public void collectors2() {
List<Integer> integers = List.of(
1, 2, 3, 4, 5, 6, 7, 8, 9);
PartitionMutableList<Integer> evenOddPartition = integers
.stream()
.collect(
Collectors2.partition(
each -> each % 2 == 0,
PartitionFastList::new));
Assertions.assertEquals(
Lists.mutable.with(2, 4, 6, 8),
evenOddPartition.getSelected());
Assertions.assertEquals(
Lists.mutable.with(1, 3, 5, 7, 9),
evenOddPartition.getRejected());
}symmetricDifference
: ThesymmetricDifference
API returns the set of all elements that are present in exactly one of the two sets. In other words, the elements that are only in one set, but not in both are returned. The example below illustrates few scenarios:@Test
public void symmetricDifference() {
MutableSet<Integer> set1 = Sets.mutable.with(1, 2, 3);
MutableSet<Integer> set2 = Sets.mutable.with(3, 4, 5);
Assertions.assertEquals(
Sets.mutable.with(1, 2, 4, 5),
set1.symmetricDifference(set2),
"3 is common element, hence not present");
Assertions.assertEquals(
set1.symmetricDifference(set2),
set2.symmetricDifference(set1),
"Symmetric Difference is commutative");
MutableSet<Integer> set3 = Sets.mutable.with(6, 7, 8);
Assertions.assertEquals(
Sets.mutable.with(1, 2, 3, 6, 7, 8),
set1.symmetricDifference(set3),
"There are no common elements");
MutableSet<Integer> set4 = Sets.mutable.with(9);
Assertions.assertEquals(
Sets.mutable.with(1, 2, 3, 9),
set1.symmetricDifference(set4),
"Sizes are different, no common elements");
MutableSet<Integer> set5 = Sets.mutable.with(1);
Assertions.assertEquals(
Sets.mutable.with(2, 3),
set1.symmetricDifference(set5),
"Sizes are different, has common elements");
Assertions.assertEquals(
Sets.mutable.empty(),
set1.symmetricDifference(set1),
"symmetricDifference with itself is empty");
}
Summary:
In this blog I explained a few lesser known features of Eclipse Collections primitiveStream()
, Immutable Primitive Collections, Collectors2()
, and symmetricDifference()
. I hope you found the post informative. If you have not used Eclipse Collections before, give it a try. There are few resources below. Make sure you show us your support and put a star on our GitHub Repository
Eclipse Collections Resources
Eclipse Collections comes with it’s own implementations of List, Set and Map. It also has additional data structures like Multimap, Bag and an entire Primitive Collections hierarchy. Each of our collections have a fluent and rich API for commonly required iteration patterns.
- Website
- Source code on GitHub (Make sure to star the Repository)
- Contribution Guide
- Reference Guide