The largest Interview Solution Library on the web


« Previous | 1 | 2 | 3 | Next »

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
1Bytes
Utility for primitive byte.
2Shorts
Utility for primitive short.
3Ints
Utility for primitive int.
4Longs
Utility for primitive long.
5Floats
Utility for primitive float.
6Doubles
Utility for primitive double.
7Chars
Utility for primitive char.
8Booleans
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
1static List<Byte> asList(byte... backingArray)
Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).
2static byte[] concat(byte[]... arrays)
Returns the values from each provided array combined into a single array.
3static boolean contains(byte[] array, byte target)
Returns true if the target is present as an element anywhere in array.
4static 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.
5static int hashCode(byte value)
Returns a hash code for value; equal to the result of invoking ((Byte) value).hashCode().
6static int indexOf(byte[] array, byte target)
Returns the index of the first appearance of the value target in array.
7static 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.
8static int lastIndexOf(byte[] array, byte target)
Returns the index of the last appearance of the value target in array.
9static 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:
  • java.lang.Object
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
1static int BYTES
The number of bytes required to represent a primitive short value.
2static short MAX_POWER_OF_TWO
The largest power of two that can be represented as a short.
Methods
S.N.Method & Description
1static List<Short> asList(short... backingArray)
Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).
2static short checkedCast(long value)
Returns the short value that is equal to value, if possible.
3static int compare(short a, short b)
Compares the two specified short values.
4static short[] concat(short[]... arrays)
Returns the values from each provided array combined into a single array.
5static boolean contains(short[] array, short target)
Returns true if target is present as an element anywhere in array.
6static 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.
7static 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().
8static 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}).
9static int hashCode(short value)
Returns a hash code for value; equal to the result of invoking ((Short) value).hashCode().
10static int indexOf(short[] array, short target)
Returns the index of the first appearance of the value target in array.
11static 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.
12static String join(String separator, short... array)
Returns a string containing the supplied short values separated by separator.
13static int lastIndexOf(short[] array, short target)
Returns the index of the last appearance of the value target in array.
14static Comparator<short[]> lexicographicalComparator()
Returns a comparator that compares two short arrays lexicographically.
15static short max(short... array)
Returns the greatest value present in array.
16static short min(short... array)
Returns the least value present in array.
17static short saturatedCast(long value)
Returns the short nearest in value to value.
18static Converter<String,Short> stringConverter()
Returns a serializable converter object that converts between strings and shorts using Short.decode(java.lang.String) and Short.toString().
19static 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().
20static 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:
  • java.lang.Object
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
1static List<Long> asList(long... backingArray)
Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).
2static int compare(long a, long b)
Compares the two specified long values.
3static long[] concat(long[]... arrays)
Returns the values from each provided array combined into a single array.
4static boolean contains(long[] array, long target)
Returns true if target is present as an element anywhere in array.
5static 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.
6static 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().
7static 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}).
8static int hashCode(long value)
Returns a hash code for value; equal to the result of invoking ((Long) value).hashCode().
9static int indexOf(long[] array, long target)
Returns the index of the first appearance of the value target in array.
10static 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.
11static String join(String separator, long... array)
Returns a string containing the supplied long values separated by separator.
12static int lastIndexOf(long[] array, long target)
Returns the index of the last appearance of the value target in array.
13static Comparator<long[]> lexicographicalComparator()
Returns a comparator that compares two long arrays lexicographically.
14static long max(long... array)
Returns the greatest value present in array.
15static long min(long... array)
Returns the least value present in array.
16static Converter<String,Long> stringConverter()
Returns a serializable converter object that converts between strings and longs using Long.decode(java.lang.String) and Long.toString().
17static 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().
18static 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().
19static Long tryParse(String string)
Parses the specified string as a signed decimal long value.
Methods Inherited

This class inherits methods from the following class:
  • java.lang.Object
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
1static int BYTES
The number of bytes required to represent a primitive float value.
Methods
S.NMethod & Description
1static List<Float> asList(float... backingArray)
Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).
2static int compare(float a, float b)
Compares the two specified float values using Float.compare(float, float).
3static float[] concat(float[]... arrays)
Returns the values from each provided array combined into a single array.
4static boolean contains(float[] array, float target)
Returns true if target is present as an element anywhere in array.
5static 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.
6static int hashCode(float value)
Returns a hash code for value; equal to the result of invoking ((Float) value).hashCode().
7static int indexOf(float[] array, float target)
Returns the index of the first appearance of the value target in array.
8static 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.
9static boolean isFinite(float value)
Returns true if value represents a real number.
10static 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.
11static int lastIndexOf(float[] array, float target)
Returns the index of the last appearance of the value target in array.
12static Comparator<float[]> lexicographicalComparator()
Returns a comparator that compares two float arrays lexicographically.
13static float max(float... array)
Returns the greatest value present in array, using the same rules of comparison as Math.min(float, float).
14static float min(float... array)
Returns the least value present in array, using the same rules of comparison as Math.min(float, float).
15static Converter<String,Float> stringConverter()
Returns a serializable converter object that converts between strings and floats using Float.valueOf(java.lang.String) and Float.toString().
16static 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().
17static 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:
  • java.lang.Object
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.NField & Description
1static int BYTES
The number of bytes required to represent a primitive double value.
Methods
S.N.Method & Description
1static List<Double> asList(double... backingArray)
Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).
2static int compare(double a, double b)
Compares the two specified double values.
3static double[] concat(double[]... arrays)
Returns the values from each provided array combined into a single array.
4static boolean contains(double[] array, double target)
Returns true if target is present as an element anywhere in array.
5static 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.
6static int hashCode(double value)
Returns a hash code for value; equal to the result of invoking ((Double) value).hashCode().
7static int indexOf(double[] array, double target)
Returns the index of the first appearance of the value target in array.
8static 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.
9static boolean isFinite(double value)
Returns true if value represents a real number.
10static 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.
11static int lastIndexOf(double[] array, double target)
Returns the index of the last appearance of the value target in array.
12static Comparator<double[]> lexicographicalComparator()
Returns a comparator that compares two double arrays lexicographically.
13static double max(double... array)
Returns the greatest value present in array, using the same rules of comparison as Math.max(double, double).
14static double min(double... array)
Returns the least value present in array, using the same rules of comparison as Math.min(double, double).
15static Converter<String,Double> stringConverter()
Returns a serializable converter object that converts between strings and doubles using Double.valueOf(java.lang.String) and Double.toString().
16static 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().
17static 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:
  • java.lang.Object
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
1static int BYTES
The number of bytes required to represent a primitive char value.
Methods
S.N.Method & Description
1static List<Character> asList(char... backingArray)
Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).
2static char checkedCast(long value)
Returns the char value that is equal to value, if possible.
3static int compare(char a, char b)
Compares the two specified char values.
4static char[] concat(char[]... arrays)
Returns the values from each provided array combined into a single array.
5static boolean contains(char[] array, char target)
Returns true if target is present as an element anywhere in array.
6static 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.
7static 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().
8static 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}).
9static int hashCode(char value)
Returns a hash code for value; equal to the result of invoking ((Character) value).hashCode().
10static int indexOf(char[] array, char target)
Returns the index of the first appearance of the value target in array.
11static 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.
12static String join(String separator, char... array)
Returns a string containing the supplied char values separated by separator.
13static int lastIndexOf(char[] array, char target)
Returns the index of the last appearance of the value target in array.
14static Comparator<char[]> lexicographicalComparator()
Returns a comparator that compares two char arrays lexicographically.
15static char max(char... array)
Returns the greatest value present in array.
16static char min(char... array)
Returns the least value present in array.
17static char saturatedCast(long value)
Returns the char nearest in value to value.
18static char[] toArray(Collection<Character> collection)
Copies a collection of Character instances into a new array of primitive char values.
19static 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:
  • java.lang.Object
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
1static List<Boolean> asList(boolean... backingArray)
Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]).
2static int compare(boolean a, boolean b)
Compares the two specified boolean values in the standard way (false is considered less than true).
3static boolean[] concat(boolean[]... arrays)
Returns the values from each provided array combined into a single array.
4static boolean contains(boolean[] array, boolean target)
Returns true if target is present as an element anywhere in array.
5static int countTrue(boolean... values)
Returns the number of values that are true.
6static 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.
7static int hashCode(boolean value)
Returns a hash code for value; equal to the result of invoking ((Boolean) value).hashCode().
8static int indexOf(boolean[] array, boolean target)
Returns the index of the first appearance of the value target in array.
9static 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.
10static String join(String separator, boolean... array)
Returns a string containing the supplied boolean values separated by separator.
11static int lastIndexOf(boolean[] array, boolean target)
Returns the index of the last appearance of the value target in array.
12static Comparator<boolean[]> lexicographicalComparator()
Returns a comparator that compares two boolean arrays lexicographically.
13static 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:
  • java.lang.Object
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
« Previous | 1 | 2 | 3 | Next »


copyright © 2014 - all rights riserved by javatechnologycenter.com