- Guava Tutorial
- Guava - Home
- Guava - Overview
- Guava - Environment Setup
- Guava - Optional Class
- Guava - Preconditions Class
- Guava - Ordering Class
- Guava - Objects Class
- Guava - Range Class
- Guava - Throwables Class
- Guava - Collections Utilities
- Guava - Caching Utilities
- Guava - String Utilities
- Guava - Primitive Utilities
- Guava - Math Utilities
- Guava Useful Resources
- Guava - Quick Guide
- Guava - Useful Resources
- Guava - Discussion
Guava - Ints Class
Ints is a utility class for primitive type int.
Class Declaration
Following is the declaration for com.google.common.primitives.Ints class −
@GwtCompatible public final class Ints extends Object
Fields
Sr.No | Field & Description |
---|---|
1 |
static int BYTES The number of bytes required to represent a primitive int value. |
2 |
static int MAX_POWER_OF_TWO The largest power of two that can be represented as a int. |
Methods
Sr.No | Method & Description |
---|---|
1 |
static List<Integer> asList(int... backingArray) Returns a fixed-size list backed by the specified array, similar to Arrays.asList(Object[]). |
2 |
static int checkedCast(long value) Returns the int value that is equal to value, if possible. |
3 |
static int compare(int a, int b) Compares the two specified int values. |
4 |
static int[] concat(int[]... arrays) Returns the values from each provided array combined into a single array. |
5 |
static boolean contains(int[] array, int target) Returns true if target is present as an element anywhere in array. |
6 |
static int[] ensureCapacity(int[] 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 fromByteArray(byte[] bytes) Returns the int value whose big-endian representation is stored in the first 4 bytes of bytes; equivalent to ByteBuffer.wrap(bytes).getInt(). |
8 |
static int fromBytes(byte b1, byte b2, byte b3, byte b4) Returns the int value whose byte representation is the given 4 bytes, in big-endian order; equivalent to Ints.fromByteArray(new byte[] {b1, b2, b3, b4}). |
9 |
static int hashCode(int value) Returns a hash code for value; equal to the result of invoking ((Integer) value).hashCode(). |
10 |
static int indexOf(int[] array, int target) Returns the index of the first appearance of the value target in array. |
11 |
static int indexOf(int[] array, int[] 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, int... array) Returns a string containing the supplied int values separated by separator. |
13 |
static int lastIndexOf(int[] array, int target) Returns the index of the last appearance of the value target in array. |
14 |
static Comparator<int[]> lexicographicalComparator() Returns a comparator that compares two int arrays lexicographically. |
15 |
static int max(int... array) Returns the greatest value present in array. |
16 |
static int min(int... array) Returns the least value present in array. |
17 |
static int saturatedCast(long value) Returns the int nearest in value to value. |
18 | static Converter<String,Integer> stringConverter() Returns a serializable converter object that converts between strings and integers using Integer.decode(java.lang.String) and Integer.toString(). |
19 |
static int[] toArray(Collection<? extends Number> collection) Returns an array containing each value of collection, converted to a int value in the manner of Number.intValue(). |
20 |
static byte[] toByteArray(int value) Returns a big-endian representation of value in a 4-element byte array; equivalent to ByteBuffer.allocate(4).putInt(value).array(). |
21 |
static Integer tryParse(String string) Parses the specified string as a signed decimal integer value. |
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