1

Google is failing me here... I thought this would be a very simple Q&A, but I cannot find any previous discussion of the matter.

Is there a reason why the Java Trove4J library does not include boolean primitive collections? Example: TByteHashSet exists, but TBooleanHashSet does not.

As a workaround, I can declare two byte constants for true(1) and false(0), but it would be more convenient to have a boolean primitive collection.

kevinarpe
  • 20,319
  • 26
  • 127
  • 154

2 Answers2

2

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.

Here's an example of creating a MutableBooleanList and ImmutableBooleanList using the BooleanLists factory.

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

Note: I am a committer for Eclipse Collections.

Donald Raab
  • 6,458
  • 2
  • 36
  • 44
1

I'm the author, so...

What would be your use case for such a TBooleanHashSet? You could only store four states:

  • empty
  • true
  • false
  • true & false

You could accomplish this easily with an EnumMap and some enum representing your states. Otherwise, the most efficient way would probably be a bit mask.

Anyway, haven't seen a need. (Note: a TBooleanList could make sense, but you could use java.util.BitSet instead.)

If you have a need, please let me know.

Rob Eden
  • 362
  • 2
  • 7
  • 1
    FYI: Goldman Sachs Collections has primitive boolean collections. Ref: http://www.goldmansachs.com/gs-collections/javadoc/4.0.0/com/gs/collections/api/set/primitive/MutableBooleanSet.html – kevinarpe Dec 02 '15 at 07:17
  • 1
    `empty, true, false, true & false` - in addition to _not set_ / _null_ / _none_ - took me a sec. – greybeard Jan 17 '16 at 14:55
  • Reading back on my old post, I'm not sure if I was trying to be that clever or if it was just a typo. Write now I'm leaning toward typo. :-) I'll fix the states... – Rob Eden Jan 18 '16 at 15:05