Saturday, October 11, 2025
HomeLanguagesJavaJava Guava | Booleans.countTrue() method with Examples

Java Guava | Booleans.countTrue() method with Examples

The countTrue() method of Booleans Class in Guava Library is used to count the number of values that are true in the specified boolean values passed as the parameter.

Syntax:

public static int countTrue(boolean... values)

Parameters: This method accepts the boolean values among which the true values are to be count.

Return values: This method returns an integer value which is the count of values that are true.

Example-1 :




// Java code to show implementation of
// Guava's Booleans.countTrue() method
  
import com.google.common.primitives.Booleans;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a Boolean array
        boolean[] arr = { false, true, false,
                          true, true };
  
        // Using Booleans.countTrue() method to
        // get the number of values that are true
        System.out.println(Booleans.countTrue(arr));
    }
}


Output:

3

Example 2 :




// Java code to show implementation of
// Guava's Booleans.countTrue() method
  
import com.google.common.primitives.Booleans;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
        // Creating a Boolean array
        boolean[] arr = { false, false,
                          false, false };
  
        // Using Booleans.countTrue() method to
        // get the number of values that are true
        System.out.println(Booleans.countTrue(arr));
    }
}


Output:

0

Reference: https://google.github.io/guava/releases/20.0/api/docs/com/google/common/primitives/Booleans.html#countTrue-boolean…-

RELATED ARTICLES

Most Popular

Dominic
32352 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6720 POSTS0 COMMENTS
Nicole Veronica
11883 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6839 POSTS0 COMMENTS
Ted Musemwa
7103 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS