Immutable Collections in Java using Sealed Types

Donald Raab
Oct 14 · 5 min read

How to define contractual, structural, and verifiable immutable Java collections.

Image for post
Image for post
Designing Immutable Collection using Sealed Types in JDK 15

Introducing Sealed Types

What is contractual immutability?

What is structural immutability?

What is verifiable immutability?

A Perfect Use Case for Sealed Types in Java

Java 9+

Google Guava

Eclipse Collections

Sealed Types

Image for post
Image for post

Similarly, we can restrict the implementations of ImmutableList.

Image for post
Image for post

The ImmutableEmptyList implementation of ImmutableList is then declared as final.

Image for post
Image for post

Experimenting with Sealed Types in JDK 15 has been interesting and encouraging. I wish this feature was available a decade ago when we first defined the ImmutableCollection hierarchy in Eclipse Collections. I’ve been able to extend the design ideas that we implemented in Eclipse Collections years ago with a feature that provides a more restrictive modeling capability.

The Deck of Cards Kata: Custom Collections

The custom collections framework interfaces and implementations can be browsed online here. The following class diagram shows the interfaces in the framework, including the immutable collection interfaces that leverage Sealed Types.

Image for post
Image for post
A Custom Collections framework in the Deck of Cards Kata

The experimental collections framework in the kata has been evolving to use Project Amber features as they become available as preview features in the JDK. The kata was upgraded to JDK 15 the day it was released. The framework now uses the following features from Project Amber:

In addition, default methods and static interface methods are used extensively to build the rich interfaces in the framework.

A vision for the future of Java Collections

The intent was to use the latest features available in the most current releases of Java where they were proved useful. The latest evolution shows what is possible by leveraging Sealed Types to implement immutable collection types. I’m quite encouraged by the results of the feature so far. I hope that this use case can be used and discussed as an example of the practical applicability of the Sealed Types feature.

The following blogs explain the evolution of the custom collections framework design over the past six months.

I hope you found this blog useful. Check out the source code and give the Deck of Cards kata and other code katas included in the repo a try.

Enjoy!

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.

Javarevisited

Medium’s largest Java publication, followed by 7860+ 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