Search code examples
javatrove4j

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


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.


Solution

  • 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.