Java Streams are great but it’s time for better Java Collections

Donald Raab
Feb 2, 2020 · 7 min read

After 25 years, it’s time for Java to get a Collections upgrade.

Image for post
Image for post
Java Streams and Collections Birds of a Feather Session from JavaOne 2014

25 Years of Java

Java 8 was and is still amazing. After 19 years without support for Lambdas, Java developers were suddenly enjoying amazing productivity gains with several great additions to the language and core libraries. Java Streams are extremely helpful and satisfy a large number of uses cases. They have also given Java content authors plenty to write about, and Java conference speakers plenty to talk about.

While the Java Collections framework has been greatly augmented with the new functionality in Java Streams, Java Streams are unfortunately hiding even greater productivity gains, increased type safety and improvements in performance.

Let’s explore why after 25 years, it is time for a better and more modern Java Collections framework. To see some of what we are still missing in Java, we can look to the past.

40 Years of Smalltalk

Smalltalk is a dynamically typed object-oriented programming language. Smalltalk has a simple syntax that can fit on a post card, and a large feature rich class library. I programmed professionally in Smalltalk from 1994–2000 while employed by IBM Global Services working at Horizon Blue Cross Blue Shield of New Jersey. Smalltalk shaped the way I think about programming over the past 25 years, and has directly impacted how I design and program in Java.

I believe one of the most overlooked features of Smalltalk is its rich Collections framework. Smalltalk collections have an organized and easy to learn hierarchy.

Image for post
Image for post
Smalltalk Collection Hierarchy

The concrete collections in Smalltalk have simple factory methods for creating them (with:) and certain collection types like arrays and strings had literal syntax. In the screen shot below I create a Set, OrderedCollection (akaList), Bag, Array, String, Dictionary (aka Map) and Interval using Pharo Smalltalk. Smalltalk is a live programming environment. In Smalltalk, you can highlight code anywhere and execute it or inspect the results and view them in an inspector.

Image for post
Image for post
Factory methods and literal syntax for creating Smalltalk collections

The collection APIs in Smalltalk are very rich, and many of the common APIs are defined a the root of the hierarchy in the Collection class.

Image for post
Image for post
APIs including select:, reject:, collect:, detect:, inject:into: available on all Collection types

To understand how rich the Smalltalk Collection API is, I have captured a mindmap for the Pharo Smalltalk Collection class showing major method categories and individual APIs. I really miss method categories in Java, as it is a great way to organize larger APIs. Every subclass of Collection inherits all of these common behaviors.

Image for post
Image for post
Smalltalk Collection class API

Smalltalk has had lambdas and a feature rich collections framework now for over 40 years — 40 years! After 25 years the JDK still does not have the same level of functionality available directly on the collections.

There are options available in the Java open source community.

16 Years of better Collections in Java

Updated: I found a link to a talk I gave at the JVM Language Summit in July 2012 titled “A Java collections framework design”. In the talk I compared the collections frameworks in Java, Scala, Smalltalk and GS Collections.

The Eclipse Collections framework was inspired from the the Smalltalk Collections framework. Eclipse Collections has factory classes which provide a consistent way of creating collections.

Image for post
Image for post

Eclipse Collections also provides a rich API, with most methods defined in a parent interface named RichIterable. The methods available directly on the collections are eager by default. Methods which return collections such as select, reject and collect are co-variant in Eclipse Collections.

Image for post
Image for post
Compare this with the Smalltalk example above

There is a lazy API available in Eclipse Collections by calling asLazy.

Image for post
Image for post

The mutable types in Eclipse Collections extend the equivalent Collection types in Java.

  • MutableList extends java.util.List
  • MutableSet extends java.util.Set
  • MutableMap extends java.util.Map
  • MutableBag extends java.util.Collection

This means that methods like stream and parallelStream are also available on the Eclipse Collections types.

Image for post
Image for post

Eclipse Collections comes with a Collectors2 class with equivalent Collector implementations to those found in Collectors but that return Eclipse Collections types.

If you want to see visualizations of the Eclipse Collections library, there are many available in the following blog.

Comparing Collection base classes in Java

Image for post
Image for post
Eclipse Collection MutableCollection compared to java.util.Collection

Note: MutableCollection extends RichIterable, which is where a lot of the behaviors for the class are inherited from.

There are a lot more features available on MutableCollection. These features result directly in productivity gains for Java developers. The MutableCollection class extends java.util.Collection so the intersections above include all of the methods in java.util.Collection and from java.lang.Iterable.

What are Java Collections missing today?

  1. Memory Efficiency
  2. Optimized Eager APIs — Learn about eager vs. lazy here
  3. Primitive Collections for all primitive types
  4. Contractually Immutable Collections
  5. Lazy Iterable APIs for object and primitive collections
  6. A *distinct* Parallel Iterable Hierarchy
  7. Multimaps
  8. Bags
  9. BiMaps
  10. Mutable and Immutable Collection Factories
  11. 64-bit collections (size is long, arrays are 64-bit)

Past is present and future is now

Let’s make Java even better for the next 25 years.

If you’re eager for these features today, there is no need to wait. You can increase your productivity and joy of programming by using Eclipse Collections right now. Eclipse Collections is available as a library in Maven Central, and has no dependencies. Eclipse Collections is an open source project at the Eclipse Foundation, which is where it gets the name prefix of Eclipse. Eclipse Collections is not related to or dependent on the Eclipse IDE and it works in any Java library, application or IDE that supports Java 8 or above.

Note: If you are still using Java 5,6 or 7, there is still hope. Eclipse Collections 7.x versions should work just fine for you. This version also works fine with Java 8. We’ve been enjoying the rich collections features of Smalltalk since JDK 1.4, which is what was available when I created the the first set of classes in the original Caramel library back in 2004.

Thank you to all of our users, contributors, committers, advocates and friends for supporting our journey in open source for the past 8 years! Your contributions, support and feedback fuels our motivation to make Eclipse Collections even better. We are working to help make the Java platform the best platform to program in for the next 25 years. Help wanted!

I am a Project Lead and Committer for the Eclipse Collections OSS project at the Eclipse Foundation. Eclipse Collections is open for contributions. If you like the library, you can let us know by starring it on GitHub.

Other Useful Resources for learning Java you may like
10 Things Java Programmer Should Learn in 2020
10 Free Courses to Learn Java from Scratch
10 Books to Learn Java in Depth
10 Tools Every Java Developer Should Know
10 Reasons to Learn Java Programming languages
10 Frameworks Java and Web Developer should learn in 2020
10 Tips to become a better Java Developer in 2020
Top 5 Java Frameworks to Learn in 2020
10 Testing Libraries Every Java Developer Should Know

Mix the sources, but keep the right balance between research and practice. And good luck to you chasing your goals, of course :)

Javarevisited

Medium’s largest Java publication, followed by 9300+ programmers. Follow to join our community.

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.

Javarevisited

A humble place to learn Java and Programming better.

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.

Javarevisited

A humble place to learn Java and Programming better.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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