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

Methods

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 of Booleans Class

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

GuavaTester.java

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("]");

      //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: " + 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

Compile the class using javac compiler as follows −

C:\Guava>javac GuavaTester.java

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
guava_primitive_utilities.htm
Advertisements