Skip to content
Related Articles

Related Articles

Booleans Class | Guava | Java

View Discussion
Improve Article
Save Article
  • Last Updated : 01 Jun, 2022

Booleans is a utility class for primitive type Boolean. It provides Static utility methods pertaining to boolean primitives, that are not already found in either Boolean or Arrays. 

Declaration : 

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

Below given are some methods provided by Guava Booleans Class :  

Exceptions :

  • ensureCapacity : IllegalArgumentException if minLength or padding is negative.
  • toArray : NullPointerException if collection or any of its elements is null.

Following table shows some other methods provided by Guava Booleans Class :  

Below given are some examples showing the implementation of Guava Booleans Class methods : 

Example 1 : 

Java




// Java code to show implementation
// of Guava Booleans.asList() method
 
import com.google.common.primitives.Booleans;
import java.util.*;
 
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        boolean arr[] = { true, false, true, false, true };
 
        // Using Booleans.asList() method which
        // converts array of primitives to array of objects
        List<Boolean> myList = Booleans.asList(arr);
 
        // Displaying the elements
        System.out.println(myList);
    }
}

Output :

[true, false, true, false, true]

Example 2 : 

Java




// Java code to show implementation
// of Guava Booleans.toArray() method
 
import com.google.common.primitives.Booleans;
import java.util.*;
 
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        List<Boolean> myList = Arrays.asList(true, false, true, false, true);
 
        // Using Booleans.toArray() method which
        // converts a List of Booleans to an
        // array of boolean
        boolean[] arr = Booleans.toArray(myList);
 
        // Displaying the elements
        System.out.println(Arrays.toString(arr));
    }
}

Output :

[true, false, true, false, true]

Example 3 : 

Java




// Java code to show implementation
// of Guava Booleans.concat() method
 
import com.google.common.primitives.Booleans;
import java.util.*;
 
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        boolean[] arr1 = { true, false, true };
        boolean[] arr2 = { false, true };
 
        // Using Booleans.concat() method which
        // combines arrays from specified
        // arrays into a single array
        boolean[] arr = Booleans.concat(arr1, arr2);
 
        // Displaying the elements
        System.out.println(Arrays.toString(arr));
    }
}

Output :

[true, false, true, false, true]

Example 4 : 

Java




// Java code to show implementation
// of Guava Booleans.contains() method
 
import com.google.common.primitives.Booleans;
 
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        boolean[] arr = { true, false, true, false, true };
 
        // Using Booleans.contains() method which
        // checks if element is present in array
        // or not
        System.out.println(Booleans.contains(arr, true));
        System.out.println(Booleans.contains(arr, false));
    }
}

output :

true
true

Example 5 : 

Java




// Java code to show implementation
// of Guava Booleans.compare() method
 
import com.google.common.primitives.Booleans;
 
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        // To compare true vs true
        System.out.println(Booleans.compare(true, true));
    }
}

Output :

0

Example 6 : 

Java




// Java code to show implementation
// of Guava Booleans.indexOf() method
 
import com.google.common.primitives.Booleans;
 
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        boolean[] arr = { true, false, true, false, true };
 
        // To print index of first occurrence of false
        System.out.println(Booleans.indexOf(arr, false));
    }
}

Output :

1

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!