Guava - Booleans Class


Booleans is a utility class for primitive type Boolean.

Class Declaration

Following is the declaration for class −

@GwtCompatible(emulated = true)
   public final class Booleans
      extends Object


Sr.No Method & Description

static List<Boolean> asList(boolean... backingArray)

Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).


static int compare(boolean a, boolean b)

Compares the two specified boolean values in the standard way (false is considered less than true).


static boolean[] concat(boolean[]... arrays)

Returns the values from each provided array combined into a single array.


static boolean contains(boolean[] array, boolean target)

Returns true if target is present as an element anywhere in array.


static int countTrue(boolean... values)

Returns the number of values that are true.


static boolean[] ensureCapacity(boolean[] array, int minLength, int padding)

Returns an array containing the same values as array, but guaranteed to be of a specified minimum length.


static int hashCode(boolean value)

Returns a hash code for value; equal to the result of invoking ((Boolean) value).hashCode().


static int indexOf(boolean[] array, boolean target)

Returns the index of the first appearance of the value target in array.


static int indexOf(boolean[] array, boolean[] target)

Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence.


static String join(String separator, boolean... array)

Returns a string containing the supplied boolean values separated by separator.


static int lastIndexOf(boolean[] array, boolean target)

Returns the index of the last appearance of the value target in array.


static Comparator<boolean[]> lexicographicalComparator()

Returns a comparator that compares two boolean arrays lexicographically.


static boolean[] toArray(Collection<Boolean> collection)

Copies a collection of Boolean instances into a new array of primitive boolean values.

Methods Inherited

This class inherits methods from the following class −

  • java.lang.Object

Example of Booleans Class

Create the following java program using any editor of your choice in say C:/> Guava

import java.util.List;

public class GuavaTester {
   public static void main(String args[]) {
      GuavaTester tester = new GuavaTester();

   private void testBooleans() {
      boolean[] booleanArray = {true,true,false,true,true,false,false};

      //convert array of primitives to array of objects
      List<Boolean> objectArray = Booleans.asList(booleanArray);

      //convert array of objects to array of primitives
      booleanArray = Booleans.toArray(objectArray);
      System.out.print("[ ");

      for(int i = 0; i< booleanArray.length ; i++) {
         System.out.print(booleanArray[i] + " ");


      //check if element is present in the list of primitives or not
      System.out.println("true is in list? " + Booleans.contains(booleanArray, true));

      //return the first index of element
      System.out.println("true position in list " + Booleans.indexOf(booleanArray, true));

      //Returns the count of true values
      System.out.println("true occured: " + Booleans.countTrue());

      //Returns the comparisons
      System.out.println("false Vs true: " +, true));
      System.out.println("false Vs false: " +, false));
      System.out.println("true Vs false: " +, false));
      System.out.println("true Vs true: " +, true));

Verify the Result

Compile the class using javac compiler as follows −


Now run the GuavaTester to see the result.

C:\Guava>java GuavaTester

See the result.

[true, true, false, true, true, false, false]
[ true true false true true false false ]
true is in list? true
true position in list 0
true occured: 0
false Vs true: -1
false Vs false: 0
true Vs false: 1
true Vs true: 0