Go 在 Java 中,或 Go 盒子。
Go Primitive in Java, or Go in a Box

原始链接: https://donraab.medium.com/go-primitive-in-java-or-go-in-a-box-c26f5c6d7574

## Eclipse Collections 与原始类型:总结 Eclipse Collections 故意缺乏某些原始类型的集合类型(如 `BiMap` 或原始类型的 `SortedSet`),这是出于设计选择和关注解决*实际*需求的结果。 值得注意的是,它避免了 `BooleanMap` 类型,尽管 Java 在 `partitioningBy()` 等方法中使用 `Map`,但仍认为它们是一种设计异味。 相反,Eclipse Collections 提倡使用 `Pair` 类型(包括原始类型/对象组合,如 `IntObjectPair`)作为 `BooleanMap` 的更安全替代方案。 该库在 Java lambda 出现*之前*(自 2012 年以来)就支持原始类型集合,并具有丰富的 API。 然而,Java 缺乏针对原始类型的泛型——这一特性可能随着 Project Valhalla 的到来而实现——阻碍了对原始类型的完全 lambda 支持。 为了不等待,Eclipse Collections 实现了诸如 `Procedure` 之类的函数式接口来处理原始类型。 Eclipse Collections 提供广泛且无与伦比的原始类型集合支持,包括可变性/不可变性选项和 lambda 集成,在十多年前就解决了这些挑战。 作者鼓励开发者*现在*就利用可用的工具,而不是等待未来的语言特性。 博客、代码练习和书籍“Eclipse Collections Categorically”等资源可供进一步学习。

相关文章

原文

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
Functional “Procedure” Interfaces for primitive types in Eclipse Collections

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.

联系我们 contact @ memedata.com