We can have our eight Java primitives and travel light in collections too.
Press enter or click to view image in full size
It’s hard to go fast when you’re in a box
Java has eight primitives. For better or worse, we’ve had them in Java for over 30 years. We use primitives all the time (e.g. loops, if-statements, math, etc.), even when we don’t use them directly (e.g. String).
Java has array type support for all eight primitives. Java has three primitive Stream types (IntStream, LongStream, DoubleStream). Java has zero primitive Collection types. You have to box primitives to use them in collections. This means wrapping boolean, byte, char, short, int, float, long, double in their object wrapper equivalents, Boolean, Byte, Character, Short, Integer, Float, Long, Double.
This is unfortunate. This is the nicest alternative I could come up with instead of saying what I really think, which is, this sucks.
I stopped caring about this “unfortunate situation” thirteen years ago. We added primitive collections to Eclipse Collections because we saw no near or distant future where primitive collection support would exist in Java natively.
We got to work and built solutions to travel light with Java collections a long time ago. You can travel light now as well if you want or need. If there’s something missing that you need, Eclipse Collections is open source and open for contributions. New contributors wanted!
It’s cheaper and faster to travel light
I could show you benchmarks and memory savings of using primitive collections instead of boxed collections. If you need to see these to be convinced of the benefits of primitive collection support in Java, then you probably don’t need support for primitive collections in Java. No need to read any further. Please accept this complimentary set of eight boxes for your collection travels.
If you understand and have a need for primitive collections, Eclipse Collections may have some solutions for you. Read on.
Eight is enough
Eclipse Collections has support for the following primitive collection types.
- List (all eight primitives)
- Set (all eight primitives)
- Stack (all eight primitives)
- Bag (all eight primitives)
- LazyIterable (all eight primitives)
- Map (all combinations except boolean as keys)
- Interval (Int and Long)
- String (char and int)
For List, Set, Stack, Bag, and Map, there are both Mutable and Immutable versions of the containers. There is only immutable primitive support for Interval and String types. LazyIterable for primitives is read-only.
Instantiate Them Using Factories
Symmetry and uniformity are very important design considerations in Eclipse Collections. While perfect symmetry is challenging to achieve, there is “good enough” symmetry for most types in the library. The following slide from the dev2next 2025 talk, “Refactoring to Eclipse Collections”, shows the combination of factories available. Credit to for creating this concise slide.
As you might notice on this slide, there are currently missing primitive collection types. There are no BiMap, Multimap, SortedBag, SortedSet, SortedMap types for primitives today. That can change over time, if folks have a specific need. We only add types to Eclipse Collections when there is a real need.
Why no primitive Boolean<V>Map?
The type Map<Boolean, V> in Java has a particular design smell. We specifically designed the primitive collection types in Eclipse Collections so there are no BooleanObjectMap<V> type or Boolean<Primitive>Map types.
Disallowing this kind of type may seem like a poor design decision to folks who enjoy Map-Oriented Programming. After all, the the Collectors.partitioningBy() method returns Map<Boolean, List<T>>, so it must be a good design right? Not all questions have a simple answer, so some questions deserve an entire blog.
In modern versions of Java (Java 17+ for LTS users), you can use a Java record to create a concise strong type for what might be considered more generally as a Pair. Eclipse Collections also has Pair, and all combinations of primitive and object Pair types (e.g. IntObjectPair, ShortBytePair, LongBytePair, etc). These are better, safer alternatives to using a Map<Boolean, V> type.
What about primitive support for lambdas?
Eclipse Collections has had primitive collection support since before Java had lambdas (around 2012). Just like the object collections in Eclipse Collections, the primitive collections were designed with a feature rich API. I knew Java would get lambdas eventually, I just wasn’t sure when exactly.
By the time we added primitive collection support to GS Collections, I believed Concise Lambda Expressions would be included in Java 8. The fundamental problem with lambda support for primitives is the same as collections support for primitives. There is no support for Generic Types over Primitives in Java today. This is a feature that may eventually arrive with Project Valhalla.
My ten year quest for lambda support in Java has been absolutely dwarfed by my twenty-one year wait for generic types over primitives.
I shared what I have been wishing and waiting for in Java in this blog.
TL;DR… This is what it looks like when you decide to stop wishing or waiting, and just get to work making a Functional Interface named Procedure/Procedure2 (aka Consumer/BiConsumer) work for the primitive types. This is only one of three Functional Interface type categories. There are also Function0/Function/Function2 and Predicate/Predicate2. The combinatorial explosion of these types is explained further in the blog and the “Eclipse Collections Categorically” book.
Press enter or click to view image in full size
Blogs, Code Katas, and other Resources
If you are interested in learning more about the primitive collection support in Eclipse Collections, the following resources can help.
Blogs
Code Katas
Lost and Found Kata in Eclipse Collections Kata repo. There is a solutions folder for this kata as well.
Book
The book “Eclipse Collections Categorically: Level up your programming game” was first published in March 2025. The book has excellent coverage of working with both object and primitive collections in Eclipse Collections. Various versions of the book are linked from the publisher here. The book is also currently available for free to Amazon Kindle Unlimited subscribers.
Reference Guide
There is an AsciiDoc Reference Guide for Eclipse Collections with a dedicated section on primitive collections here.
Final Thoughts
The extensive primitive collections support in Eclipse Collections has been one of its most popular features. The combination of primitive collections with lambda-enabled API and support for mutable and immutable types is unmatched in any other Java collections library. These are hard problems to solve, but they have been solved problems in Eclipse Collections for well over a decade.
It will be great when Project Valhalla is finally realized and released in Java. Maybe you can afford to wait for Project Valhalla to arrive and finally build the applications and libraries you really want to build. I’m glad we got to work on supporting primitive collections in Eclipse Collections when I was in my early forties. Now I’m in my mid-fifties, and I have decided I’m getting too old to wait for language miracles to arrive.
Java has been good enough since Java 8, and gets better with every release. I go primitive any time I need to. I don’t need to wait for anything.
You can either get to work using what’s available today, or wait and hope for someone to eventually unbox the box you’ve been travelling in. Go primitive in Java, or go in a box. Your choice.
Thanks for reading!
I am the creator of and committer for the Eclipse Collections OSS project, which is managed at the Eclipse Foundation. Eclipse Collections is open for contributions. I am the author of the book, Eclipse Collections Categorically: Level up your programming game.
.png)
