Friday, September 19, 2025
HomeLanguagesJavaJava Guava | Booleans.asList() method with Examples

Java Guava | Booleans.asList() method with Examples

The Booleans.asList() method of Guava’s Booleans Class accepts a boolean array as a parameter and returns a list which has the fixed size. The returned list is backed by the boolean array which is passed as the argument. It means the changes made in the array passed as parameter will be reflected back in the list returned by the method and vice-versa.

Syntax:

public static List<Boolean> asList(boolean… backingArray)

Parameter: The method accepts a mandatory parameter backingArray which is a boolean array that is used to back the list.

Return Value: The method Booleans.asList() returns a fixed-size list which is backed by the array passed as the argument to the method. In other words, the method returns the list view of the array.

Below examples illustrate the implementation of above method:

Example 1:




// Java code to show implementation of
// Guava's Booleans.asList() method
  
import com.google.common.primitives.Booleans;
import java.util.List;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a Boolean array
        boolean arr[] = { true, false, true,
                          true, true };
  
        // Using Booleans.asList() method to wrap
        // the specified primitive Boolean array
        // as a List of the Boolean type
        List<Boolean> myList = Booleans.asList(arr);
  
        // Displaying the elements in List
        System.out.println(myList);
    }
}


Output:

[true, false, true, true, true]

Example 2:




// Java code to show implementation of
// Guava's Booleans.asList() method
  
import com.google.common.primitives.Booleans;
import java.util.List;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating a Boolean array
        boolean arr[] = { false, true, false };
  
        // Using Booleans.asList() method to wrap
        // the specified primitive Boolean array
        // as a List of the Boolean type
        List<Boolean> myList = Booleans.asList(arr);
  
        // Displaying the elements in List
        System.out.println(myList);
    }
}


Output:

[false, true, false]

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

RELATED ARTICLES

Most Popular

Dominic
32303 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6666 POSTS0 COMMENTS
Nicole Veronica
11841 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11898 POSTS0 COMMENTS
Shaida Kate Naidoo
6781 POSTS0 COMMENTS
Ted Musemwa
7059 POSTS0 COMMENTS
Thapelo Manthata
6740 POSTS0 COMMENTS
Umr Jansen
6745 POSTS0 COMMENTS