Fastest way to check if an array of boolean contains true

后端 未结 7 1073
余生分开走
余生分开走 2021-01-12 04:08

I have an array of boolean entries:

boolean[] myBooleanArray = new boolean[24];

Currently i check if it contains

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-12 04:33

    If you are using the Guava library (which has a lot of useful stuff):

    Booleans.contains(myBooleanArray, true);
    

    (JavaDoc)

    The documentation of this method also describes another way. You can replace a boolean[] with a BitSet (should be more memory efficient) and call !bitSet.isEmpty() to check whether at least one bit is true.

提交回复
热议问题