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
import com.google.common.primitives.Booleans;
import java.util.*;
class GFG {
public static void main(String[] args)
{
boolean arr[] = { true , false , true , false , true };
List<Boolean> myList = Booleans.asList(arr);
System.out.println(myList);
}
}
|
Output :
[true, false, true, false, true]
Example 2 :
Java
import com.google.common.primitives.Booleans;
import java.util.*;
class GFG {
public static void main(String[] args)
{
List<Boolean> myList = Arrays.asList( true , false , true , false , true );
boolean [] arr = Booleans.toArray(myList);
System.out.println(Arrays.toString(arr));
}
}
|
Output :
[true, false, true, false, true]
Example 3 :
Java
import com.google.common.primitives.Booleans;
import java.util.*;
class GFG {
public static void main(String[] args)
{
boolean [] arr1 = { true , false , true };
boolean [] arr2 = { false , true };
boolean [] arr = Booleans.concat(arr1, arr2);
System.out.println(Arrays.toString(arr));
}
}
|
Output :
[true, false, true, false, true]
Example 4 :
Java
import com.google.common.primitives.Booleans;
class GFG {
public static void main(String[] args)
{
boolean [] arr = { true , false , true , false , true };
System.out.println(Booleans.contains(arr, true ));
System.out.println(Booleans.contains(arr, false ));
}
}
|
output :
true
true
Example 5 :
Java
import com.google.common.primitives.Booleans;
class GFG {
public static void main(String[] args)
{
System.out.println(Booleans.compare( true , true ));
}
}
|
Output :
0
Example 6 :
Java
import com.google.common.primitives.Booleans;
class GFG {
public static void main(String[] args)
{
boolean [] arr = { true , false , true , false , true };
System.out.println(Booleans.indexOf(arr, false ));
}
}
|
Output :
1
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
01 Jun, 2022
Like Article
Save Article