10 Features in Eclipse Collections 10.0

Image for post
Image for post
The sun has set on the Eclipse Collections 10.0 release.

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.

2. toMap with a target Map

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

3. RichIterable toBiMap

With Eclipse Collections 10.0, you can now convert any RichIterable to a 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.

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.

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. Specify 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.

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.

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

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.

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.

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.

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.

Written by

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

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store