10 Features in Eclipse Collections 10.0

Donald Raab
Aug 1 · 4 min read

My Top 10 List

In this post, I will share my favorite ten features included in the Eclipse Collections 10.0 release.

1. Stream for Primitive Lists

Now you can call primitiveStream on primitive Lists. The method primitiveStream is available on Int, Long and DoubleLists.

public void primitiveListToPrimitiveStream()
IntStream intStream1 =
IntLists.mutable.with(1, 2, 3, 4, 5)
IntStream intStream2 =
IntLists.immutable.with(1, 2, 3, 4, 5)
LongStream longStream1 =
LongLists.mutable.with(1L, 2L, 3L, 4L, 5L)
LongStream longStream2 =
LongLists.immutable.with(1L, 2L, 3L, 4L, 5L)
DoubleStream doubleStream1 =
DoubleLists.mutable.with(1.0, 2.0, 3.0, 4.0, 5.0)
DoubleStream doubleStream2 =
DoubleLists.immutable.with(1.0, 2.0, 3.0, 4.0, 5.0)

2. toMap with a target Map

The method toMap has been overloaded to allow a target map to be passed in as a parameter.

public void toMapWithTarget()
MutableList<Integer> list =
Lists.mutable.with(1, 2, 3, 4, 5);
Map<String, Integer> map =
each -> each,
new LinkedHashMap<>());
Map<String, Integer> expected = new LinkedHashMap<>();
expected.put("1", 1);
expected.put("2", 2);
expected.put("3", 3);
expected.put("4", 4);
expected.put("5", 5);
Assert.assertEquals(expected, map);

3. RichIterable toBiMap

With Eclipse Collections 10.0, you can now convert any RichIterable to a BiMap.

public void toBiMap()
MutableBiMap<String, Integer> expected =
BiMaps.mutable.with("1", 1, "2", 2, "3", 3);
MutableBiMap<String, Integer> biMap =
Lists.mutable.with(1, 2, 3).toBiMap(String::valueOf, i -> i);
Assert.assertEquals(expected, biMap);

4. fromStream on Collection Factories

We can now construct a Collection from a Stream using fromStream on each of the Collection factories for List, Set, Bag, and Stack.

public void fromStreamOnCollectionFactories()
list = Lists.mutable.fromStream(Stream.of(1, 2, 3, 4, 5));
Lists.mutable.with(1, 2, 3, 4, 5), list);
MutableSet<Integer> set =
Sets.mutable.fromStream(Stream.of(1, 2, 3, 4, 5));
Sets.mutable.with(1, 2, 3, 4, 5), set);
MutableBag<Integer> bag =
Bags.mutable.fromStream(Stream.of(1, 2, 3, 4, 5));
Bags.mutable.with(1, 2, 3, 4, 5), bag);
MutableStack<Integer> stack =
Stacks.mutable.fromStream(Stream.of(1, 2, 3, 4, 5));
Stacks.mutable.with(1, 2, 3, 4, 5), stack);

5. MutableMultimap getIfAbsentPutAll

The method getIfAbsentPutAll on a MutableMultimap is equivalent to getIfAbsentPut on a MutableMap. The difference is that with a Multimap you can put in multiple values.

public void getIfAbsentPutAll()
MutableListMultimap<Integer, Integer> multimap =
Multimaps.mutable.list.with(2, 1);
ImmutableList<Integer> defaultValue =
Lists.immutable.with(1, 2, 3);
MutableList<Integer> oneValue =
multimap.getIfAbsentPutAll(1, defaultValue);
MutableList<Integer> twoValue =
multimap.getIfAbsentPutAll(2, defaultValue);
Assert.assertEquals(defaultValue, oneValue);
Assert.assertEquals(Lists.mutable.with(1), twoValue);

6. Bag collectWithOccurrences

You can use this method to transform a collection using all of the unique items in the bag along with their counts. You specific an ObjectIntToObjectFunction to transform the items an their counts to some resulting object. In the following example, I will collect the items and their counts into ObjectIntPair instances.

public void collectWithOccurences()
MutableBag<String> source =
Bags.mutable.with("1", "2", "2", "3", "3", "3");
MutableBag<ObjectIntPair<String>> targetBag =
MutableBag<ObjectIntPair<String>> expected =
PrimitiveTuples.pair("1", 1),
PrimitiveTuples.pair("2", 2),
PrimitiveTuples.pair("3", 3));
Assert.assertEquals(expected, targetBag);

7. Reduce / reduceIfEmpty for Primitive Iterables

The method reduce will apply a two argument (long, int) function which returns a long for each element of the collection. This allows for a widening of the result type so as not to overflow on functions like sum. In the case of an empty collection, a NoSuchElementException will be thrown.

public void reducePrimitiveIterables()
MutableIntList list =
IntLists.mutable.with(1, 2, 3, 4, 5);
long sum = list.reduce(Long::sum); Assert.assertEquals(15L, sum);
Assert.assertEquals(list.sum(), sum);
()-> IntLists.mutable.empty().reduce(Long::sum));

If you would like to safely handle the case of empty, you can use reduceIfEmpty and specify a default value to return.

public void reduceIfEmptyPrimitiveIterables()
MutableIntList list =
IntLists.mutable.with(1, 2, 3, 4, 5);
long sum = list.reduceIfEmpty(Long::sum, 0L); Assert.assertEquals(15L, sum);
Assert.assertEquals(list.sum(), sum);
.reduceIfEmpty(Long::sum, 0L));

8. RichIterable countByEach

The method countByEach is similar to groupByEach and flatCollect. All three take a Function which returns an Iterable. The result in the case of countByEach is a Bag. In the following example, I count all of the methods by their names for three classes. The count of methods will include overloads and overrides of the methods.

public void countByEach()
MutableList<Class<?>> classes =
Bag<String> methodNames =
classes.countByEach(each ->

9. Create Primitive Map from Iterable

You can now create a primitive Map from an Iterable with two provided Functions. One Function is used to calculate the key, and the other is used to calculate the value. This method is very similar the method toMap on RichIterable. The difference is that it works with any primitive Map.

public void createPrimitiveMapFromIterable()
Iterable<Integer> integers = Interval.oneTo(5);
MutableIntIntMap map =
key -> key,
value -> value);
MutableIntIntMap expected = IntIntMaps.mutable.empty()
.withKeyValue(1, 1)
.withKeyValue(2, 2)
.withKeyValue(3, 3)
.withKeyValue(4, 4)
.withKeyValue(5, 5);
Assert.assertEquals(expected, map);

10. Convert Iterable to Primitive Collections

Before Eclipse Collections 10.0, if you wanted to convert an Iterable of boxed primitive types to a primitive Collection (e.g. Integer -> int), you would have to first convert the Iterable to a Collection or a Stream. In the case of a Stream, you could then use one of the stock primitive Collectors in Collectors2 to convert to a primitive Collection. Now you can use the following factory methods to convert from Iterable of some boxed value like Integer to a primitive Collection. This works for all of the boxed primitive types, across all supported primitive Collection types.

public void convertFromIterableToPrimitiveCollection()
Iterable<Integer> iterable = Interval.oneTo(5);
IntInterval intInterval = IntInterval.oneTo(5);

MutableIntList mIntList =
ImmutableIntList iIntList =

Assert.assertEquals(intInterval, mIntList);
Assert.assertEquals(intInterval, iIntList);

MutableIntSet mIntSet =
ImmutableIntSet iIntSet =

Assert.assertEquals(intInterval.toSet(), mIntSet);
Assert.assertEquals(intInterval.toSet(), iIntSet);

MutableIntBag mIntBag =
ImmutableIntBag iIntBag =

Assert.assertEquals(intInterval.toBag(), mIntBag);
Assert.assertEquals(intInterval.toBag(), iIntBag);

I hope you enjoy all of the new features in Eclipse Collections 10.0!

Eclipse Collections is open for contributions. If you like the library, you can let us know by starring it on GitHub.

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