Guava - PRIMITIVE UTILITIES
As primitive types of Java cannot be used to pass in generics or in collections as input, Guava provided a lot of Wrapper Utilities classes to handle primitive types as Objects. Following is the list of useful primitive processing utilities:
| S.N. | Utility name & Description |
| 1 | Bytes Utility for primitive byte. |
| 2 | Shorts Utility for primitive short. |
| 3 | Ints Utility for primitive int. |
| 4 | Longs Utility for primitive long. |
| 5 | Floats Utility for primitive float. |
| 6 | Doubles Utility for primitive double. |
| 7 | Chars Utility for primitive char. |
| 8 | Booleans Utility for primitive boolean. |
Bytes Class
Bytes is a utility class for primitive type byte.
Class Declaration
Following is the declaration for
com.google.common.primitives.Bytes class:
@GwtCompatible
public final class Bytes
extends Object
Methods
| S.N. | Method & Description |
| 1 | static List<Byte> asList(byte... backingArray) Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]). |
| 2 | static byte[] concat(byte[]... arrays) Returns the values from each provided array combined into a single array. |
| 3 | static boolean contains(byte[] array, byte target) Returns true if the target is present as an element anywhere in array. |
| 4 | static byte[] ensureCapacity(byte[] array, int minLength, int padding) Returns an array containing the same values as the array, but guaranteed to be of a specified minimum length. |
| 5 | static int hashCode(byte value) Returns a hash code for value; equal to the result of invoking ((Byte) value).hashCode(). |
| 6 | static int indexOf(byte[] array, byte target) Returns the index of the first appearance of the value target in array. |
| 7 | static int indexOf(byte[] array, byte[] target) Returns the start position of the first occurrence of the specified target within the array, or -1 if there is no such occurrence. |
| 8 | static int lastIndexOf(byte[] array, byte target) Returns the index of the last appearance of the value target in array. |
| 9 | static byte[] toArray(Collection<? extends Number> collection) Returns an array containing each value of collection, converted to a byte value in the manner of Number.byteValue(). |
Methods Inherited
This class inherits methods from the following class:
Example of Bytes 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.Bytes;
public class GuavaTester {
public static void main(String args[]){
GuavaTester tester = new GuavaTester();
tester.testBytes();
}
private void testBytes(){
byte[] byteArray = {1,2,3,4,5,5,7,9,9};
//convert array of primitives to array of objects
List<Byte> objectArray = Bytes.asList(byteArray);
System.out.println(objectArray.toString());
//convert array of objects to array of primitives
byteArray = Bytes.toArray(objectArray);
System.out.print("[ ");
for(int i = 0; i< byteArray.length ; i++){
System.out.print(byteArray[i] + " ");
}
System.out.println("]");
byte data = 5;
//check if element is present in the list of primitives or not
System.out.println("5 is in list? "+ Bytes.contains(byteArray,
data));
//Returns the index
System.out.println("Index of 5: " + Bytes.indexOf(byteArray,data));
//Returns the last index maximum
System.out.println("Last index of 5: " +
Bytes.lastIndexOf(byteArray,data));
}
}
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.
[1, 2, 3, 4, 5, 5, 7, 9, 9]
[ 1 2 3 4 5 5 7 9 9 ]
5 is in list? true
Index of 5: 4
Last index of 5: 5
Shorts Class
Shorts is a utility class for primitive type short.
Class Declaration
Following is the declaration for
com.google.common.primitives.Shorts class:
@GwtCompatible
public final class Shorts
extends Object
Fields
| S.N. | Field & Description |
| 1 | static int BYTES The number of bytes required to represent a primitive short value. |
| 2 | static short MAX_POWER_OF_TWO The largest power of two that can be represented as a short. |
Methods
| S.N. | Method & Description |
| 1 | static List<Short> asList(short... backingArray) Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]). |
| 2 | static short checkedCast(long value) Returns the short value that is equal to value, if possible. |
| 3 | static int compare(short a, short b) Compares the two specified short values. |
| 4 | static short[] concat(short[]... arrays) Returns the values from each provided array combined into a single array. |
| 5 | static boolean contains(short[] array, short target) Returns true if target is present as an element anywhere in array. |
| 6 | static short[] ensureCapacity(short[] array, int minLength, int padding) Returns an array containing the same values as array, but guaranteed to be of a specified minimum length. |
| 7 | static short fromByteArray(byte[] bytes) Returns the short value whose big-endian representation is stored in the first 2 bytes of bytes; equivalent to ByteBuffer.wrap(bytes).getShort(). |
| 8 | static short fromBytes(byte b1, byte b2) Returns the short value whose byte representation is the given 2 bytes, in big-endian order; equivalent to Shorts.fromByteArray(new byte[] {b1, b2}). |
| 9 | static int hashCode(short value) Returns a hash code for value; equal to the result of invoking ((Short) value).hashCode(). |
| 10 | static int indexOf(short[] array, short target) Returns the index of the first appearance of the value target in array. |
| 11 | static int indexOf(short[] array, short[] target) Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence. |
| 12 | static String join(String separator, short... array) Returns a string containing the supplied short values separated by separator. |
| 13 | static int lastIndexOf(short[] array, short target) Returns the index of the last appearance of the value target in array. |
| 14 | static Comparator<short[]> lexicographicalComparator() Returns a comparator that compares two short arrays lexicographically. |
| 15 | static short max(short... array) Returns the greatest value present in array. |
| 16 | static short min(short... array) Returns the least value present in array. |
| 17 | static short saturatedCast(long value) Returns the short nearest in value to value. |
| 18 | static Converter<String,Short> stringConverter() Returns a serializable converter object that converts between strings and shorts using Short.decode(java.lang.String) and Short.toString(). |
| 19 | static short[] toArray(Collection<? extends Number> collection) Returns an array containing each value of collection, converted to a short value in the manner of Number.shortValue(). |
| 20 | static byte[] toByteArray(short value) Returns a big-endian representation of value in a 2-element byte array; equivalent to ByteBuffer.allocate(2).putShort(value).array(). |
Methods Inherited
This class inherits methods from the following class:
Example of Ints 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.Ints;
public class GuavaTester {
public static void main(String args[]){
GuavaTester tester = new GuavaTester();
tester.testInts();
}
private void testInts(){
int[] intArray = {1,2,3,4,5,6,7,8,9};
//convert array of primitives to array of objects
List<Integer> objectArray = Ints.asList(intArray);
System.out.println(objectArray.toString());
//convert array of objects to array of primitives
intArray = Ints.toArray(objectArray);
System.out.print("[ ");
for(int i = 0; i< intArray.length ; i++){
System.out.print(intArray[i] + " ");
}
System.out.println("]");
//check if element is present in the list of primitives or not
System.out.println("5 is in list? "+ Ints.contains(intArray, 5));
//Returns the minimum
System.out.println("Min: " + Ints.min(intArray));
//Returns the maximum
System.out.println("Max: " + Ints.max(intArray));
//get the byte array from an integer
byte[] byteArray = Ints.toByteArray(20000);
for(int i = 0; i< byteArray.length ; i++){
System.out.print(byteArray[i] + " ");
}
}
}
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.
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[ 1 2 3 4 5 6 7 8 9 ]
5 is in list? true
Min: 1
Max: 9
0 0 78 32
Longs Class
Longs is a utility class for primitive type long.
Class Declaration
Following is the declaration for
com.google.common.primitives.Longs class:
@GwtCompatible
public final class Longs
extends Object
Fields
| S.N. | Method & Description |
| 1 | static List<Long> asList(long... backingArray) Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]). |
| 2 | static int compare(long a, long b) Compares the two specified long values. |
| 3 | static long[] concat(long[]... arrays) Returns the values from each provided array combined into a single array. |
| 4 | static boolean contains(long[] array, long target) Returns true if target is present as an element anywhere in array. |
| 5 | static long[] ensureCapacity(long[] array, int minLength, int padding) Returns an array containing the same values as array, but guaranteed to be of a specified minimum length. |
| 6 | static long fromByteArray(byte[] bytes) Returns the long value whose big-endian representation is stored in the first 8 bytes of bytes; equivalent to ByteBuffer.wrap(bytes).getLong(). |
| 7 | static long fromBytes(byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7, byte b8) Returns the long value whose byte representation is the given 8 bytes, in big-endian order; equivalent to Longs.fromByteArray(new byte[] {b1, b2, b3, b4, b5, b6, b7, b8}). |
| 8 | static int hashCode(long value) Returns a hash code for value; equal to the result of invoking ((Long) value).hashCode(). |
| 9 | static int indexOf(long[] array, long target) Returns the index of the first appearance of the value target in array. |
| 10 | static int indexOf(long[] array, long[] target) Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence. |
| 11 | static String join(String separator, long... array) Returns a string containing the supplied long values separated by separator. |
| 12 | static int lastIndexOf(long[] array, long target) Returns the index of the last appearance of the value target in array. |
| 13 | static Comparator<long[]> lexicographicalComparator() Returns a comparator that compares two long arrays lexicographically. |
| 14 | static long max(long... array) Returns the greatest value present in array. |
| 15 | static long min(long... array) Returns the least value present in array. |
| 16 | static Converter<String,Long> stringConverter() Returns a serializable converter object that converts between strings and longs using Long.decode(java.lang.String) and Long.toString(). |
| 17 | static long[] toArray(Collection<? extends Number> collection) Returns an array containing each value of collection, converted to a long value in the manner of Number.longValue(). |
| 18 | static byte[] toByteArray(long value) Returns a big-endian representation of value in an 8-element byte array; equivalent to ByteBuffer.allocate(8).putLong(value).array(). |
| 19 | static Long tryParse(String string) Parses the specified string as a signed decimal long value. |
Methods Inherited
This class inherits methods from the following class:
Example of Longs 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.Ints;
import com.google.common.primitives.Longs;
public class GuavaTester {
public static void main(String args[]){
GuavaTester tester = new GuavaTester();
tester.testLongs();
}
private void testLongs(){
long[] longArray = {1,2,3,4,5,6,7,8,9};
//convert array of primitives to array of objects
List<Long> objectArray = Longs.asList(longArray);
System.out.println(objectArray.toString());
//convert array of objects to array of primitives
longArray = Longs.toArray(objectArray);
System.out.print("[ ");
for(int i = 0; i< longArray.length ; i++){
System.out.print(longArray[i] + " ");
}
System.out.println("]");
//check if element is present in the list of primitives or not
System.out.println("5 is in list? "+ Longs.contains(longArray, 5));
//Returns the minimum
System.out.println("Min: " + Longs.min(longArray));
//Returns the maximum
System.out.println("Max: " + Longs.max(longArray));
//get the byte array from an integer
byte[] byteArray = Longs.toByteArray(20000);
for(int i = 0; i< byteArray.length ; i++){
System.out.print(byteArray[i] + " ");
}
}
}
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.
[1, 2, 3, 4, 5, 6, 7, 8, 9]
[ 1 2 3 4 5 6 7 8 9 ]
5 is in list? true
Min: 1
Max: 9
0 0 0 0 0 0 78 32
Floats Class
Floats is a utility class for primitive type float.
Class Declaration
Following is the declaration for
com.google.common.primitives.Floats class:
@GwtCompatible(emulated=true)
public final class Floats
extends Object
Fields
| S.N. | Field & Description |
| 1 | static int BYTES The number of bytes required to represent a primitive float value. |
Methods
| S.N | Method & Description |
| 1 | static List<Float> asList(float... backingArray) Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]). |
| 2 | static int compare(float a, float b) Compares the two specified float values using Float.compare(float, float). |
| 3 | static float[] concat(float[]... arrays) Returns the values from each provided array combined into a single array. |
| 4 | static boolean contains(float[] array, float target) Returns true if target is present as an element anywhere in array. |
| 5 | static float[] ensureCapacity(float[] array, int minLength, int padding) Returns an array containing the same values as the array, but guaranteed to be of a specified minimum length. |
| 6 | static int hashCode(float value) Returns a hash code for value; equal to the result of invoking ((Float) value).hashCode(). |
| 7 | static int indexOf(float[] array, float target) Returns the index of the first appearance of the value target in array. |
| 8 | static int indexOf(float[] array, float[] target) Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence. |
| 9 | static boolean isFinite(float value) Returns true if value represents a real number. |
| 10 | static String join(String separator, float... array) Returns a string containing the supplied float values, converted to strings as specified by Float.toString(float), and separated by separator. |
| 11 | static int lastIndexOf(float[] array, float target) Returns the index of the last appearance of the value target in array. |
| 12 | static Comparator<float[]> lexicographicalComparator() Returns a comparator that compares two float arrays lexicographically. |
| 13 | static float max(float... array) Returns the greatest value present in array, using the same rules of comparison as Math.min(float, float). |
| 14 | static float min(float... array) Returns the least value present in array, using the same rules of comparison as Math.min(float, float). |
| 15 | static Converter<String,Float> stringConverter() Returns a serializable converter object that converts between strings and floats using Float.valueOf(java.lang.String) and Float.toString(). |
| 16 | static float[] toArray(Collection<? extends Number> collection) Returns an array containing each value of collection, converted to a float value in the manner of Number.floatValue(). |
| 17 | static Float tryParse(String string) Parses the specified string as a single-precision floating point value. |
Methods Inherited
This class inherits methods from the following class:
Example of Floats 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.Floats;
public class GuavaTester {
public static void main(String args[]){
GuavaTester tester = new GuavaTester();
tester.testFloats();
}
private void testFloats(){
float[] floatArray =
{1.0f,2.0f,3.0f,4.0f,5.0f,6.0f,7.0f,8.0f,9.0f};
//convert array of primitives to array of objects
List<Float> objectArray = Floats.asList(floatArray);
System.out.println(objectArray.toString());
//convert array of objects to array of primitives
floatArray = Floats.toArray(objectArray);
System.out.print("[ ");
for(int i = 0; i< floatArray.length ; i++){
System.out.print(floatArray[i] + " ");
}
System.out.println("]");
//check if element is present in the list of primitives or not
System.out.println("5.0 is in list? "+ Floats.contains(floatArray,
5.0f));
//return the index of element
System.out.println("5.0 position in list "+
Floats.indexOf(floatArray, 5.0f));
//Returns the minimum
System.out.println("Min: " + Floats.min(floatArray));
//Returns the maximum
System.out.println("Max: " + Floats.max(floatArray));
}
}
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.
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
[ 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ]
5.0 is in list? true
5.0 position in list 4
Min: 1.0
Max: 9.0
Doubles Class
Doubles is a utility class for primitive type double.
Class Declaration
Following is the declaration for
com.google.common.primitives.Doubles class:
@GwtCompatible(emulated=true)
public final class Doubles
extends Object
Fields
| S.N | Field & Description |
| 1 | static int BYTES The number of bytes required to represent a primitive double value. |
Methods
| S.N. | Method & Description |
| 1 | static List<Double> asList(double... backingArray) Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]). |
| 2 | static int compare(double a, double b) Compares the two specified double values. |
| 3 | static double[] concat(double[]... arrays) Returns the values from each provided array combined into a single array. |
| 4 | static boolean contains(double[] array, double target) Returns true if target is present as an element anywhere in array. |
| 5 | static double[] ensureCapacity(double[] array, int minLength, int padding) Returns an array containing the same values as the array, but guaranteed to be of a specified minimum length. |
| 6 | static int hashCode(double value) Returns a hash code for value; equal to the result of invoking ((Double) value).hashCode(). |
| 7 | static int indexOf(double[] array, double target) Returns the index of the first appearance of the value target in array. |
| 8 | static int indexOf(double[] array, double[] target) Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence. |
| 9 | static boolean isFinite(double value) Returns true if value represents a real number. |
| 10 | static String join(String separator, double... array) Returns a string containing the supplied double values, converted to strings as specified by Double.toString(double), and separated by separator. |
| 11 | static int lastIndexOf(double[] array, double target) Returns the index of the last appearance of the value target in array. |
| 12 | static Comparator<double[]> lexicographicalComparator() Returns a comparator that compares two double arrays lexicographically. |
| 13 | static double max(double... array) Returns the greatest value present in array, using the same rules of comparison as Math.max(double, double). |
| 14 | static double min(double... array) Returns the least value present in array, using the same rules of comparison as Math.min(double, double). |
| 15 | static Converter<String,Double> stringConverter() Returns a serializable converter object that converts between strings and doubles using Double.valueOf(java.lang.String) and Double.toString(). |
| 16 | static double[] toArray(Collection<? extends Number> collection) Returns an array containing each value of collection, converted to a double value in the manner of Number.doubleValue(). |
| 17 | static Double tryParse(String string) Parses the specified string as a double-precision floating point value. |
Methods Inherited
This class inherits methods from the following class:
Example of Doubles 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.Doubles;
public class GuavaTester {
public static void main(String args[]){
GuavaTester tester = new GuavaTester();
tester.testDoubles();
}
private void testDoubles(){
double[] doubleArray = {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0};
//convert array of primitives to array of objects
List<Double> objectArray = Doubles.asList(doubleArray);
System.out.println(objectArray.toString());
//convert array of objects to array of primitives
doubleArray = Doubles.toArray(objectArray);
System.out.print("[ ");
for(int i = 0; i< doubleArray.length ; i++){
System.out.print(doubleArray[i] + " ");
}
System.out.println("]");
//check if element is present in the list of primitives or not
System.out.println("5.0 is in list? "+
Doubles.contains(doubleArray, 5.0f));
//return the index of element
System.out.println("5.0 position in list "+
Doubles.indexOf(doubleArray, 5.0f));
//Returns the minimum
System.out.println("Min: " + Doubles.min(doubleArray));
//Returns the maximum
System.out.println("Max: " + Doubles.max(doubleArray));
}
}
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.
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
[ 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0 ]
5.0 is in list? true
5.0 position in list 4
Min: 1.0
Max: 9.0
Chars Class
Chars is a utility class for primitive type char.
Class Declaration
Following is the declaration for
com.google.common.primitives.Chars class:
@GwtCompatible(emulated=true)
public final class Chars
extends Object
Fields
| S.N. | Field & Description |
| 1 | static int BYTES The number of bytes required to represent a primitive char value. |
Methods
| S.N. | Method & Description |
| 1 | static List<Character> asList(char... backingArray) Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]). |
| 2 | static char checkedCast(long value) Returns the char value that is equal to value, if possible. |
| 3 | static int compare(char a, char b) Compares the two specified char values. |
| 4 | static char[] concat(char[]... arrays) Returns the values from each provided array combined into a single array. |
| 5 | static boolean contains(char[] array, char target) Returns true if target is present as an element anywhere in array. |
| 6 | static char[] ensureCapacity(char[] array, int minLength, int padding) Returns an array containing the same values as array, but guaranteed to be of a specified minimum length. |
| 7 | static char fromByteArray(byte[] bytes) Returns the char value whose big-endian representation is stored in the first 2 bytes of bytes; equivalent to ByteBuffer.wrap(bytes).getChar(). |
| 8 | static char fromBytes(byte b1, byte b2) Returns the char value whose byte representation is the given 2 bytes, in big-endian order; equivalent to Chars.fromByteArray(new byte[] {b1, b2}). |
| 9 | static int hashCode(char value) Returns a hash code for value; equal to the result of invoking ((Character) value).hashCode(). |
| 10 | static int indexOf(char[] array, char target) Returns the index of the first appearance of the value target in array. |
| 11 | static int indexOf(char[] array, char[] target) Returns the start position of the first occurrence of the specified target within array, or -1 if there is no such occurrence. |
| 12 | static String join(String separator, char... array) Returns a string containing the supplied char values separated by separator. |
| 13 | static int lastIndexOf(char[] array, char target) Returns the index of the last appearance of the value target in array. |
| 14 | static Comparator<char[]> lexicographicalComparator() Returns a comparator that compares two char arrays lexicographically. |
| 15 | static char max(char... array) Returns the greatest value present in array. |
| 16 | static char min(char... array) Returns the least value present in array. |
| 17 | static char saturatedCast(long value) Returns the char nearest in value to value. |
| 18 | static char[] toArray(Collection<Character> collection) Copies a collection of Character instances into a new array of primitive char values. |
| 19 | static byte[] toByteArray(char value) Returns a big-endian representation of value in a 2-element byte array; equivalent to ByteBuffer.allocate(2).putChar(value).array(). |
Methods Inherited
This class inherits methods from the following class:
Example of Chars 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.Chars;
public class GuavaTester {
public static void main(String args[]){
GuavaTester tester = new GuavaTester();
tester.testChars();
}
private void testChars(){
char[] charArray = {'a','b','c','d','e','f','g','h'};
//convert array of primitives to array of objects
List<Character> objectArray = Chars.asList(charArray);
System.out.println(objectArray.toString());
//convert array of objects to array of primitives
charArray = Chars.toArray(objectArray);
System.out.print("[ ");
for(int i = 0; i< charArray.length ; i++){
System.out.print(charArray[i] + " ");
}
System.out.println("]");
//check if element is present in the list of primitives or not
System.out.println("c is in list? "+ Chars.contains(charArray, 'c'));
//return the index of element
System.out.println("c position in list "+ Chars.indexOf(charArray, 'c'));
//Returns the minimum
System.out.println("Min: " + Chars.min(charArray));
//Returns the maximum
System.out.println("Max: " + Chars.max(charArray));
}
}
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.
[a, b, c, d, e, f, g, h]
[ a b c d e f g h ]
c is in list? true
c position in list 2
Min: a
Max: h
Booleans Class
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
| S.N. | Method & Description |
| 1 | static List<Boolean> asList(boolean... backingArray) Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]). |
| 2 | static int compare(boolean a, boolean b) Compares the two specified boolean values in the standard way (false is considered less than true). |
| 3 | static boolean[] concat(boolean[]... arrays) Returns the values from each provided array combined into a single array. |
| 4 | static boolean contains(boolean[] array, boolean target) Returns true if target is present as an element anywhere in array. |
| 5 | static int countTrue(boolean... values) Returns the number of values that are true. |
| 6 | static 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. |
| 7 | static int hashCode(boolean value) Returns a hash code for value; equal to the result of invoking ((Boolean) value).hashCode(). |
| 8 | static int indexOf(boolean[] array, boolean target) Returns the index of the first appearance of the value target in array. |
| 9 | static 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. |
| 10 | static String join(String separator, boolean... array) Returns a string containing the supplied boolean values separated by separator. |
| 11 | static int lastIndexOf(boolean[] array, boolean target) Returns the index of the last appearance of the value target in array. |
| 12 | static Comparator<boolean[]> lexicographicalComparator() Returns a comparator that compares two boolean arrays lexicographically. |
| 13 | static 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:
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