After 25 years, it’s time for Java to get a Collections upgrade.
25 Years of Java
This year will mark the 25th anniversary of the Java programming language. In March 2020, we will celebrate the 6th anniversary of the release of Java 8 which included Lambdas, Method References, Default Methods, and Java Streams.
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
Forty years ago, Smalltalk-80 was released. Smalltalk has influenced so much that we use on a daily basis in modern programming languages and software development techniques like TDD and Refactoring.
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.
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
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.
The collection APIs in Smalltalk are very rich, and many of the common APIs are defined a the root of the hierarchy in the
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.
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
I started developing a library named Caramel while working in Goldman Sachs in 2004. In 2012, the library was open sourced as GS Collections on GitHub. In 2015 it was migrated to the Eclipse Foundation and became Eclipse Collections.
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.
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
collect are co-variant in Eclipse Collections.
There is a lazy API available in Eclipse Collections by calling
The mutable types in Eclipse Collections extend the equivalent Collection types in Java.
This means that methods like
parallelStream are also available on the Eclipse Collections types.
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.
Visualizing Eclipse Collections
A visual overview of the APIs, Interfaces, Factories, Static Utility and Adapters in Eclipse Collections using mind…
Comparing Collection base classes in Java
So what is the difference between Eclipse Collections and the JDK Collections framework? In the image below I have calculated the difference and intersection of the
java.util.Collection APIs using method names without parameters, so overloaded methods are excluded.
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
What are Java Collections missing today?
- Functional and fluent APIs directly on the collections
- Memory Efficiency
- Optimized Eager APIs — Learn about eager vs. lazy here
- Primitive Collections for all primitive types
- Contractually Immutable Collections
- Lazy Iterable APIs for object and primitive collections
- A *distinct* Parallel Iterable Hierarchy
- Mutable and Immutable Collection Factories
- 64-bit collections (size is long, arrays are 64-bit)
Past is present and future is now
We have a lot we can still learn in Java from Smalltalk. Smalltalk was (arguably still is) way ahead of its time. Adding additional syntax in newer versions of the Java language may be useful, but we need to build much better support in the base class libraries like the Collections framework. I hope to see many of these classic collections features added in a future version of Java. Java is a great development platform, and an updated Collections framework will make Java even better.
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 :)