Логический класс | Гуава | Ява

Опубликовано: 13 Февраля, 2022

Booleans - это служебный класс для примитивного типа Boolean. Он предоставляет статические служебные методы, относящиеся к логическим примитивам, которые еще не найдены ни в Boolean, ни в массивах.

Декларация:

@GwtCompatible (emulated = true)
открытый конечный класс Booleans
расширяет объект

Ниже приведены некоторые методы, предоставляемые классом Guava Booleans:

Исключения:

  • sureCapacity: IllegalArgumentException, если minLength или padding отрицательны.
  • toArray: NullPointerException, если коллекция или любой из ее элементов имеет значение NULL.

Following table shows some other methods provided by Guava Booleans Class :

Below given are some examples showing the implementation of Guava Booleans Class methods :
Example 1 :

// Java code to show implementation
// of Guava Booleans.asList() method
  
import com.google.common.primitives.Booleans;
import java.util.*;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        boolean arr[] = { true, false, true, false, true };
  
        // Using Booleans.asList() method which
        // converts array of primitives to array of objects
        List<Boolean> myList = Booleans.asList(arr);
  
        // Displaying the elements
        System.out.println(myList);
    }
}

Выход :

[true, false, true, false, true]

Example 2 :

// Java code to show implementation
// of Guava Booleans.toArray() method
  
import com.google.common.primitives.Booleans;
import java.util.*;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        List<Boolean> myList = Arrays.asList(true, false, true, false, true);
  
        // Using Booleans.toArray() method which
        // converts a List of Booleans to an
        // array of boolean
        boolean[] arr = Booleans.toArray(myList);
  
        // Displaying the elements
        System.out.println(Arrays.toString(arr));
    }
}

Выход :

[истина, ложь, истина, ложь, истина]

Example 3 :

// Java code to show implementation
// of Guava Booleans.concat() method
  
import com.google.common.primitives.Booleans;
import java.util.*;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        boolean[] arr1 = { true, false, true };
        boolean[] arr2 = { false, true };
  
        // Using Booleans.concat() method which
        // combines arrays from specified
        // arrays into a single array
        boolean[] arr = Booleans.concat(arr1, arr2);
  
        // Displaying the elements
        System.out.println(Arrays.toString(arr));
    }
}

Выход :

[истина, ложь, истина, ложь, истина]

Example 4 :

// Java code to show implementation
// of Guava Booleans.contains() method
  
import com.google.common.primitives.Booleans;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        boolean[] arr = { true, false, true, false, true };
  
        // Using Booleans.contains() method which
        // checks if element is present in array
        // or not
        System.out.println(Booleans.contains(arr, true));
        System.out.println(Booleans.contains(arr, false));
    }
}

выход :

правда
правда

Example 5 :

// Java code to show implementation
// of Guava Booleans.compare() method
  
import com.google.common.primitives.Booleans;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        // To compare true vs true
        System.out.println(Booleans.compare(true, true));
    }
}

Выход :

0

Example 6 :

// Java code to show implementation
// of Guava Booleans.indexOf() method
  
import com.google.common.primitives.Booleans;
  
class GFG {
    // Driver method
    public static void main(String[] args)
    {
        boolean[] arr = { true, false, true, false, true };
  
        // To print index of first occurence of false
        System.out.println(Booleans.indexOf(arr, false));
    }
}

Выход :

1

Вниманию читателя! Не переставай учиться сейчас. Ознакомьтесь со всеми важными концепциями Java Foundation и коллекций с помощью курса "Основы Java и Java Collections" по доступной для студентов цене и будьте готовы к работе в отрасли. Чтобы завершить подготовку от изучения языка к DS Algo и многому другому, см. Полный курс подготовки к собеседованию .