Guava - Useful Classes
- Guava - Optional Class
- Guava - Preconditions Class
- Guava - Ordering Class
- Guava - Objects Class
- Guava - Range Class
- Guava - Throwables Class
- Guava - LoadingCache Interface
Guava - Collection Utilities
- Guava - Collections Utilities
- Guava - MultiSet Interface
- Guava - MultiMap Interface
- Guava - BiMap Interface
- Guava - Table Interface
Guava - String Utilities
- Guava - String Utilities
- Guava - Joiner class
- Guava - Splitter
- Guava - CharMatcher
- Guava - CaseFormat
Guava - Primitive Utilities
- Guava - Primitive Utilities
- Guava - Bytes
- Guava - Shorts
- Guava - Ints
- Guava - Longs
- Guava - Floats
- Guava - Doubles
- Guava - Chars
- Guava - Booleans
Guava - Math Utilities
Guava - Useful Resources
Guava - Booleans Class
Booleans is a utility class for primitive type Boolean.
Class Declaration
Following is the declaration for com.google.common.primitives.Booleans class −
@GwtCompatible(emulated = true)
public final class Booleans
extends Object
| Sr.No | Method & Description |
|---|---|
| 1 |
static List<Boolean> asList(boolean... backingArray) Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]). |
| 2 |
static int compare(boolean a, boolean b) Compares the two specified boolean values in the standard way (false is considered less than true). |
| 3 |
static boolean[] concat(boolean[]... arrays) Returns the values from each provided array combined into a single array. |
| 4 |
static boolean contains(boolean[] array, boolean target) Returns true if target is present as an element anywhere in array. |
| 5 |
static int countTrue(boolean... values) Returns the number of values that are true. |
| 6 |
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. |
| 7 |
static int hashCode(boolean value) Returns a hash code for value; equal to the result of invoking ((Boolean) value).hashCode(). |
| 8 |
static int indexOf(boolean[] array, boolean target) Returns the index of the first appearance of the value target in array. |
| 9 |
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. |
| 10 |
static String join(String separator, boolean... array) Returns a string containing the supplied boolean values separated by separator. |
| 11 |
static int lastIndexOf(boolean[] array, boolean target) Returns the index of the last appearance of the value target in array. |
| 12 |
static Comparator<boolean[]> lexicographicalComparator() Returns a comparator that compares two boolean arrays lexicographically. |
| 13 |
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 - Converting Array of Booleans to List and vice versa
GuavaTester.java
package com.tutorialspoint;
import java.util.List;
import com.google.common.primitives.Booleans;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testBooleans();
}
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);
System.out.println(objectArray.toString());
//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] + " ");
}
System.out.println("]");
}
}
Verify the Result
Run the GuavaTester and verify the output −
[true, true, false, true, true, false, false] [ true true false true true false false ]
Example - Checking Presence of Element in Arrays of Booleans
GuavaTester.java
package com.tutorialspoint;
import com.google.common.primitives.Booleans;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testBooleans();
}
private void testBooleans() {
boolean[] booleanArray = {true,true,false,true,true,false,false};
//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(booleanArray));
}
}
Verify the Result
Run the GuavaTester and verify the output −
true is in list? true true position in list 0 true occured: 4
Example - Comparing boolean values
GuavaTester.java
package com.tutorialspoint;
import com.google.common.primitives.Booleans;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testBooleans();
}
private void testBooleans() {
//Returns the comparisons
System.out.println("false Vs true: " + Booleans.compare(false, true));
System.out.println("false Vs false: " + Booleans.compare(false, false));
System.out.println("true Vs false: " + Booleans.compare(true, false));
System.out.println("true Vs true: " + Booleans.compare(true, true));
}
}
Verify the Result
Run the GuavaTester and verify the output −
false Vs true: -1 false Vs false: 0 true Vs false: 1 true Vs true: 0