Nine Features in Eclipse Collections 9.0

Donald Raab
Sep 14, 2017 · 4 min read

1. CountBy

// Eclipse Collections 7.x - 8.x
MutableBag<String> countsOld =
this.company.getCustomers()
.asLazy().collect(Customer::getCity).toBag();
// Eclipse Collections 9.x
MutableBag<String> countsNew =
this.company.getCustomers()
.countBy(Customer::getCity);

2. DistinctBy

// Eclipse Collections 7.x - 8.x
MutableList<Customer> distinctOld =
this.company.getCustomers()
.distinct(
HashingStrategies.fromFunction(Customer::getCity));
// Eclipse Collections 9.x
MutableList<Customer> distinctNew =
this.company.getCustomers()
.distinctBy(Customer::getCity);

3. Primitive Collection Factories work with Primitive Java Streams

// Eclipse Collections 7.x - 8.x
MutableIntList listOld =
IntStream.rangeClosed(1, 100)
.collect(
IntLists.mutable::empty,
MutableIntList::add,
MutableIntList::withAll);
// Eclipse Collections 9.x
MutableIntList listNew =
IntLists.mutable.withAll(
IntStream.rangeClosed(1, 100));

4. Factory classes can now create adapters

// Eclipse Collections 7.x - 8.x
MutableList<Object> listAdapter =
ListAdapter.adapt(new ArrayList<>());
MutableSet<Object> setAdapter =
SetAdapter.adapt(new ConcurrentSkipListSet<>());
MutableMap<Object, Object> mapAdapter =
MapAdapter.adapt(new LinkedHashMap<>());
// Eclipse Collections 9.x
MutableList<Object> listAdapter =
Lists.adapt(new ArrayList<>());
MutableSet<Object> setAdapter =
Sets.adapt(new ConcurrentSkipListSet<>());
MutableMap<Object, Object> mapAdapter =
Maps.adapt(new LinkedHashMap<>());

5. Streams available directly on Immutable Collections

// Eclipse Collections 7.x - 8.x
boolean result =
Lists.immutable.with(1, 2, 3)
.castToList()
.stream()
.anyMatch(i -> i % 2 == 0);
// Eclipse Collections 9.x
boolean result =
Lists.immutable.with(1, 2, 3)
.stream()
.anyMatch(i -> i % 2 == 0);

6. FlatCollect on primitive lazy iterables

// Eclipse Collections 8.x
IntList listOne = IntLists.mutable.with(1, 2, 3);
IntList listTwo = IntLists.mutable.with(4, 5, 6);
MutableList<IntIntPair> pairs = listOne
.flatCollect(i ->
listTwo.collect(j ->
PrimitiveTuples.pair(i, j)),
Lists.mutable.empty());
// Eclipse Collections 9.x
IntList listOne = IntLists.mutable.with(1, 2, 3);
IntList listTwo = IntLists.mutable.with(4, 5, 6);
LazyIterable<IntIntPair> pairs =
listOne.asLazy()
.flatCollect(i -> listTwo.asLazy()
.collect(j -> PrimitiveTuples.pair(i, j)));

7. Streams available for values on all Object Valued Maps

// Eclipse Collections 7.x - 8.x - Object Maps
boolean result =
Maps.mutable.with(1, 1, 2, 2, 3, 3)
.values()
.stream()
.anyMatch(i -> i % 2 == 0);
// Eclipse Collections 9.x - Object Maps
boolean result =
Maps.mutable.with(1, 1, 2, 2, 3, 3)
.stream()
.anyMatch(i -> i % 2 == 0);
// Eclipse Collections 7.x - 8.x - PrimitiveObject Maps
boolean primitiveResult =
IntObjectMaps.mutable.<Integer>empty()
.withKeyValue(1, 1)
.withKeyValue(2, 2)
.withKeyValue(3, 3)
.values()
.stream()
.anyMatch(i -> i % 2 == 0);
// Eclipse Collections 9.x - PrimitiveObject Maps
boolean primitiveResult2 =
IntObjectMaps.mutable.<Integer>empty()
.withKeyValue(1, 1)
.withKeyValue(2, 2)
.withKeyValue(3, 3)
.stream()
.anyMatch(i -> i % 2 == 0);

8. AverageIfEmpty and MedianIfEmpty on primitive iterables

// Eclipse Collections 7.x - 8.x
double average = IntLists.mutable.empty().average();
// throws java.lang.ArithmeticException
double median = IntLists.mutable.empty().median();
// throws java.lang.ArithmeticException
// Eclipse Collections 9.x
double average = IntLists.mutable.empty().averageIfEmpty(0.0);
double median = IntLists.mutable.empty().medianIfEmpty(0.0);

9. Primitive Sets now have Cartesian Product

// Eclipse Collections 7.x - 8.x
Set<Integer> a = Sets.mutable.with(1, 2, 3);
Set<Integer> b = Sets.mutable.with(4, 5, 6);
LazyIterable<Pair<Integer, Integer>> pairs =
Sets.cartesianProduct(a, b);
// Eclipse Collections 9.x
IntSet a = IntSets.mutable.with(1, 2, 3);
IntSet b = IntSets.mutable.with(4, 5, 6);
LazyIterable<IntIntPair> pairs =
IntSets.cartesianProduct(a, b);

Donald Raab

Written by

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

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade