Java Trove4J 库(或其他库)是否有布尔原始集合?

Does Java Trove4J library (or another) have boolean primitive collections?

Google 让我失望了...我认为这将是一个非常简单的问答,但我找不到任何以前对此事的讨论。

Java Trove4J 库不包含布尔原始集合是否有原因?示例:TByteHashSet 存在,但 TBooleanHashSet 不存在。

作为变通方法,我可以为 true(1) 和 false(0) 声明两个字节常量,但使用布尔原始集合会更方便。

我是作者,所以...

对于这样的 TBooleanHashSet,您的用例是什么?您只能存储四种状态:

  • 正确
  • 错误
  • 正确与错误

您可以使用 EnumMap 和代表您的状态的一些枚举轻松完成此操作。否则,最有效的方法可能是位掩码。

总之,没看到有必要。 (注意:TBooleanList 可能有意义,但您可以改用 java.util.BitSet。)

如果您有需要,请告诉我。

Eclipse Collections has BooleanSet, BooleanList, BooleanStack, BooleanBag and primitive maps with boolean as values. There are both Mutable and Immutable versions. You can find all subinterfaces of BooleanIterable here. Factory classes for the different primitive containers are here.

下面是使用 BooleanLists 工厂创建 MutableBooleanListImmutableBooleanList 的示例。

MutableBooleanList mutable = BooleanLists.mutable.with(true, false, true, false);
ImmutableBooleanList immutable = BooleanLists.immutable.with(true, false, true, false);
Assert.assertEquals(mutable, immutable);

注意:我是 Eclipse Collections 的提交者。