Guava – Booleans Class

  • Post author:
  • Post category:Guava
  • Post comments:0 Comments
Booleans

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.NoMethod & Description
1static List<Boolean> asList(boolean… backingArray)Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).
2static int compare(boolean a, boolean b)Compares the two specified boolean values in the standard way (false is considered less than true).
3static boolean[] concat(boolean[]… arrays)Returns the values from each provided array combined into a single array.
4static boolean contains(boolean[] array, boolean target)Returns true if target is present as an element anywhere in array.
5static int countTrue(boolean… values)Returns the number of values that are true.
6static 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.
7static int hashCode(boolean value)Returns a hash code for value; equal to the result of invoking ((Boolean) value).hashCode().
8static int indexOf(boolean[] array, boolean target)Returns the index of the first appearance of the value target in array.
9static 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.
10static String join(String separator, boolean… array)Returns a string containing the supplied boolean values separated by separator.
11static int lastIndexOf(boolean[] array, boolean target)Returns the index of the last appearance of the value target in array.
12static Comparator<boolean[]> lexicographicalComparator()Returns a comparator that compares two boolean arrays lexicographically.
13static 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

Previous Page:-Click Here

Leave a Reply