Guava - Useful Classes
- Guava - Optional Class
- Guava - Preconditions Class
- Guava - Ordering Class
- Guava - Objects Class
- Guava - Range Class
- Guava - Throwables Class
- Guava - LoadingCache Interface
Guava - Collection Utilities
- Guava - Collections Utilities
- Guava - MultiSet Interface
- Guava - MultiMap Interface
- Guava - BiMap Interface
- Guava - Table Interface
Guava - String Utilities
- Guava - String Utilities
- Guava - Joiner class
- Guava - Splitter
- Guava - CharMatcher
- Guava - CaseFormat
Guava - Primitive Utilities
- Guava - Primitive Utilities
- Guava - Bytes
- Guava - Shorts
- Guava - Ints
- Guava - Longs
- Guava - Floats
- Guava - Doubles
- Guava - Chars
- Guava - Booleans
Guava - Math Utilities
Guava - Useful Resources
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 −
Useful Primitive Utilities
| Sr.No | 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. |
Example - Converting Byte Array to List and vice versa
GuavaTester.java
package com.tutorialspoint;
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("]");
}
}
Output
Run the GuavaTester and verify the output −
[1, 2, 3, 4, 5, 5, 7, 9, 9] [ 1 2 3 4 5 5 7 9 9 ]
Example - Check if Element is present in Short Array
GuavaTester.java
package com.tutorialspoint;
import com.google.common.primitives.Shorts;
public class GuavaTester {
public static void main(String args[]) {
GuavaTester tester = new GuavaTester();
tester.testShorts();
}
private void testShorts() {
short[] shortArray = {1,2,3,4,5,6,7,8,9};
short data = 5;
// check if element is present in the list of primitives or not
System.out.println("5 is in list? " + Shorts.contains(shortArray, data));
data = 6;
System.out.println("6 is in list? " + Shorts.contains(shortArray, data));
}
}
Output
Run the GuavaTester and verify the output −
5 is in list? true 6 is in list? false
Example - Getting Minimum/Maximum of Int Array
GuavaTester.java
package com.tutorialspoint;
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};
//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
Run the GuavaTester and verify the output −
Min: 1 Max: 9 0 0 78 32
Advertisements