Class Booleans
- java.lang.Object
- 
- com.google.common.primitives.Booleans
 
- 
 @GwtCompatible public final class Booleans extends java.lang.Object Static utility methods pertaining tobooleanprimitives, that are not already found in eitherBooleanorArrays.See the Guava User Guide article on primitive utilities. - Since:
- 1.0
- Author:
- Kevin Bourrillion
 
- 
- 
Method SummaryAll Methods Static Methods Concrete Methods Modifier and Type Method Description static java.util.List<java.lang.Boolean>asList(boolean... backingArray)Returns a fixed-size list backed by the specified array, similar toArrays.asList(Object[]).static intcompare(boolean a, boolean b)Compares the two specifiedbooleanvalues in the standard way (falseis considered less thantrue).static boolean[]concat(boolean[]... arrays)Returns the values from each provided array combined into a single array.static booleancontains(boolean[] array, boolean target)Returnstrueiftargetis present as an element anywhere inarray.static intcountTrue(boolean... values)Returns the number ofvaluesthat aretrue.static boolean[]ensureCapacity(boolean[] array, int minLength, int padding)Returns an array containing the same values asarray, but guaranteed to be of a specified minimum length.static java.util.Comparator<java.lang.Boolean>falseFirst()Returns aComparator<Boolean>that sortsfalsebeforetrue.static inthashCode(boolean value)Returns a hash code forvalue; equal to the result of invoking((Boolean) value).hashCode().static intindexOf(boolean[] array, boolean target)Returns the index of the first appearance of the valuetargetinarray.static intindexOf(boolean[] array, boolean[] target)Returns the start position of the first occurrence of the specifiedtargetwithinarray, or-1if there is no such occurrence.static java.lang.Stringjoin(java.lang.String separator, boolean... array)Returns a string containing the suppliedbooleanvalues separated byseparator.static intlastIndexOf(boolean[] array, boolean target)Returns the index of the last appearance of the valuetargetinarray.static java.util.Comparator<boolean[]>lexicographicalComparator()Returns a comparator that compares twobooleanarrays lexicographically.static voidreverse(boolean[] array)Reverses the elements ofarray.static voidreverse(boolean[] array, int fromIndex, int toIndex)Reverses the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive.static voidrotate(boolean[] array, int distance)Performs a right rotation ofarrayof "distance" places, so that the first element is moved to index "distance", and the element at indexiends up at index(distance + i) mod array.length.static voidrotate(boolean[] array, int distance, int fromIndex, int toIndex)Performs a right rotation ofarraybetweenfromIndexinclusive andtoIndexexclusive.static boolean[]toArray(java.util.Collection<java.lang.Boolean> collection)Copies a collection ofBooleaninstances into a new array of primitivebooleanvalues.static java.util.Comparator<java.lang.Boolean>trueFirst()Returns aComparator<Boolean>that sortstruebeforefalse.
 
- 
- 
- 
Method Detail- 
trueFirstpublic static java.util.Comparator<java.lang.Boolean> trueFirst() Returns aComparator<Boolean>that sortstruebeforefalse.This is particularly useful in Java 8+ in combination with Comparator.comparing, e.g.Comparator.comparing(Foo::hasBar, trueFirst()).- Since:
- 21.0
 
 - 
falseFirstpublic static java.util.Comparator<java.lang.Boolean> falseFirst() Returns aComparator<Boolean>that sortsfalsebeforetrue.This is particularly useful in Java 8+ in combination with Comparator.comparing, e.g.Comparator.comparing(Foo::hasBar, falseFirst()).- Since:
- 21.0
 
 - 
hashCodepublic static int hashCode(boolean value) Returns a hash code forvalue; equal to the result of invoking((Boolean) value).hashCode().Java 8+ users: use Boolean.hashCode(boolean)instead.- Parameters:
- value- a primitive- booleanvalue
- Returns:
- a hash code for the value
 
 - 
compare@InlineMe(replacement="Boolean.compare(a, b)") public static int compare(boolean a, boolean b) Compares the two specifiedbooleanvalues in the standard way (falseis considered less thantrue). The sign of the value returned is the same as that of((Boolean) a).compareTo(b).Note: this method is now unnecessary and should be treated as deprecated; use the equivalent Boolean.compare(boolean, boolean)method instead.- Parameters:
- a- the first- booleanto compare
- b- the second- booleanto compare
- Returns:
- a positive number if only aistrue, a negative number if onlybis true, or zero ifa == b
 
 - 
containspublic static boolean contains(boolean[] array, boolean target) Returnstrueiftargetis present as an element anywhere inarray.Note: consider representing the array as a BitSetinstead, replacingBooleans.contains(array, true)with!bitSet.isEmpty()andBooleans.contains(array, false)withbitSet.nextClearBit(0) == sizeOfBitSet.- Parameters:
- array- an array of- booleanvalues, possibly empty
- target- a primitive- booleanvalue
- Returns:
- trueif- array[i] == targetfor some value of- i
 
 - 
indexOfpublic static int indexOf(boolean[] array, boolean target) Returns the index of the first appearance of the valuetargetinarray.Note: consider representing the array as a BitSetinstead, and usingBitSet.nextSetBit(int)orBitSet.nextClearBit(int).- Parameters:
- array- an array of- booleanvalues, possibly empty
- target- a primitive- booleanvalue
- Returns:
- the least index ifor whicharray[i] == target, or-1if no such index exists.
 
 - 
indexOfpublic static int indexOf(boolean[] array, boolean[] target) Returns the start position of the first occurrence of the specifiedtargetwithinarray, or-1if there is no such occurrence.More formally, returns the lowest index isuch thatArrays.copyOfRange(array, i, i + target.length)contains exactly the same elements astarget.- Parameters:
- array- the array to search for the sequence- target
- target- the array to search for as a sub-sequence of- array
 
 - 
lastIndexOfpublic static int lastIndexOf(boolean[] array, boolean target) Returns the index of the last appearance of the valuetargetinarray.- Parameters:
- array- an array of- booleanvalues, possibly empty
- target- a primitive- booleanvalue
- Returns:
- the greatest index ifor whicharray[i] == target, or-1if no such index exists.
 
 - 
concatpublic static boolean[] concat(boolean[]... arrays) Returns the values from each provided array combined into a single array. For example,concat(new boolean[] {a, b}, new boolean[] {}, new boolean[] {c}returns the array{a, b, c}.- Parameters:
- arrays- zero or more- booleanarrays
- Returns:
- a single array containing all the values from the source arrays, in order
- Throws:
- java.lang.IllegalArgumentException- if the total number of elements in- arraysdoes not fit in an- int
 
 - 
ensureCapacitypublic static boolean[] ensureCapacity(boolean[] array, int minLength, int padding) Returns an array containing the same values asarray, but guaranteed to be of a specified minimum length. Ifarrayalready has a length of at leastminLength, it is returned directly. Otherwise, a new array of sizeminLength + paddingis returned, containing the values ofarray, and zeroes in the remaining places.- Parameters:
- array- the source array
- minLength- the minimum length the returned array must guarantee
- padding- an extra amount to "grow" the array by if growth is necessary
- Returns:
- an array containing the values of array, with guaranteed minimum lengthminLength
- Throws:
- java.lang.IllegalArgumentException- if- minLengthor- paddingis negative
 
 - 
joinpublic static java.lang.String join(java.lang.String separator, boolean... array) Returns a string containing the suppliedbooleanvalues separated byseparator. For example,join("-", false, true, false)returns the string"false-true-false".- Parameters:
- separator- the text that should appear between consecutive values in the resulting string (but not at the start or end)
- array- an array of- booleanvalues, possibly empty
 
 - 
lexicographicalComparatorpublic static java.util.Comparator<boolean[]> lexicographicalComparator() Returns a comparator that compares twobooleanarrays lexicographically. That is, it compares, usingcompare(boolean, boolean)), the first pair of values that follow any common prefix, or when one array is a prefix of the other, treats the shorter array as the lesser. For example,[] < [false] < [false, true] < [true].The returned comparator is inconsistent with Object.equals(Object)(since arrays support only identity equality), but it is consistent withArrays.equals(boolean[], boolean[]).- Since:
- 2.0
 
 - 
toArraypublic static boolean[] toArray(java.util.Collection<java.lang.Boolean> collection) Copies a collection ofBooleaninstances into a new array of primitivebooleanvalues.Elements are copied from the argument collection as if by collection.toArray(). Calling this method is as thread-safe as calling that method.Note: consider representing the collection as a BitSetinstead.- Parameters:
- collection- a collection of- Booleanobjects
- Returns:
- an array containing the same values as collection, in the same order, converted to primitives
- Throws:
- java.lang.NullPointerException- if- collectionor any of its elements is null
 
 - 
asListpublic static java.util.List<java.lang.Boolean> asList(boolean... backingArray) Returns a fixed-size list backed by the specified array, similar toArrays.asList(Object[]). The list supportsList.set(int, Object), but any attempt to set a value tonullwill result in aNullPointerException.There are at most two distinct objects in this list, (Boolean) trueand(Boolean) false. Java guarantees that those are always represented by the same objects.The returned list is serializable. - Parameters:
- backingArray- the array to back the list
- Returns:
- a list view of the array
 
 - 
countTruepublic static int countTrue(boolean... values) Returns the number ofvaluesthat aretrue.- Since:
- 16.0
 
 - 
reversepublic static void reverse(boolean[] array) Reverses the elements ofarray. This is equivalent toCollections.reverse(Booleans.asList(array)), but is likely to be more efficient.- Since:
- 23.1
 
 - 
reversepublic static void reverse(boolean[] array, int fromIndex, int toIndex) Reverses the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive. This is equivalent toCollections.reverse(Booleans.asList(array).subList(fromIndex, toIndex)), but is likely to be more efficient.- Throws:
- java.lang.IndexOutOfBoundsException- if- fromIndex < 0,- toIndex > array.length, or- toIndex > fromIndex
- Since:
- 23.1
 
 - 
rotatepublic static void rotate(boolean[] array, int distance) Performs a right rotation ofarrayof "distance" places, so that the first element is moved to index "distance", and the element at indexiends up at index(distance + i) mod array.length. This is equivalent toCollections.rotate(Booleans.asList(array), distance), but is somewhat faster.The provided "distance" may be negative, which will rotate left. - Since:
- 32.0.0
 
 - 
rotatepublic static void rotate(boolean[] array, int distance, int fromIndex, int toIndex) Performs a right rotation ofarraybetweenfromIndexinclusive andtoIndexexclusive. This is equivalent toCollections.rotate(Booleans.asList(array).subList(fromIndex, toIndex), distance), but is somewhat faster.The provided "distance" may be negative, which will rotate left. - Throws:
- java.lang.IndexOutOfBoundsException- if- fromIndex < 0,- toIndex > array.length, or- toIndex > fromIndex
- Since:
- 32.0.0
 
 
- 
 
-