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 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output:

3

Example 2 :

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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));
    }
}

chevron_right


Output:

0

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



My Personal Notes arrow_drop_up

In love with a semicolon because sometimes i miss it so badly)

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.