Guide to Ints class of Google Guava

1. Overview

Ints class in Google Guava provides several static utilities not available in Arrays or Integer class. As primitive types cannot be used as generic and hence general purpose utilities cannot be applied to them. Let’s take a look at plethora of methods provided in Ints class.

2. Methods

2.1 Ints.hashCode

Get a hash code of the int value. It returns hash code equivalent to Integer.valueof(42).hashCode(). As of Java 8 it is recommended to use Integer.hashCode(int) method.

int val = 42;
int hashCode = Ints.hashCode(val);
Assert.assertEquals(42, hashCode);

2.2 Ints.checkedCast

Cast long value to int value. If the long value is out of range of int value the method throws IllegalArgumentException.

long lVal = 546342312L;
int iVal = Ints.checkedCast(lVal);
Assert.assertEquals(546342312, iVal);

2.3 Ints.checkedCast

Tries to convert out of range long value resulting in IllegalArgumentException.

@Test(expected = IllegalArgumentException.class)
public void checkedCastException() {
    long lVal = 2147483648L;
    int iVal = Ints.checkedCast(lVal);
    Ints.checkedCast(lVal);
}

2.4 Ints.saturatedCast

Saturated cast method will cast out-of-range positive value to Integer.MAX_VALUE subsequently it will cast out-of-range negative value to Integer.MIN_VALUE

long lVal = 3_147_483_648L;
int iVal = Ints.saturatedCast(lVal);
int result = Ints.saturatedCast(lVal);
Assert.assertEquals(Integer.MAX_VALUE, result);

2.5 Ints.contains

Searches for the target element in arr. Returns true if found, returns false otherwise.

int[] arr = {6, 7, 8, 5, 6, 2};
Assert.assertTrue(Ints.contains(arr, 6));
Assert.assertFalse(Ints.contains(arr, 23));

2.6 Ints.indexOf

Returns index of target element in arr. Returns index if found, returns -1 otherwise.

int[] arr = {6, 7, 8, 5, 6, 2};
Assert.assertEquals(0, Ints.indexOf(arr, 6));
Assert.assertEquals(-1, Ints.indexOf(arr, 68));

2.7 Ints.lastIndexOf

Returns last index of target element in arr. Returns last index if found, returns -1 otherwise.

int[] arr = {6, 7, 8, 5, 6, 2};
Assert.assertEquals(4, Ints.lastIndexOf(arr, 6));
Assert.assertEquals(-1, Ints.lastIndexOf(arr, 68));

2.8 Ints.min

Returns minimum values present in arr.

int[] arr = {6, 7, 8, 5, 6, 2};
Assert.assertEquals(2, Ints.min(arr));

2.9 Ints.max

Returns maximum values present in arr.

int[] arr = {6, 7, 8, 5, 6, 2};
Assert.assertEquals(8, Ints.max(arr));

2.10 Ints.concat

Concats all the arrays into one array in order.

int[] arr1 = {1, 2, 3};
int[] arr2 = {4, 5, 6};
int[] arr3 = {7, 8, 9};
int[] result = Ints.concat(arr1, arr2, arr3);
Assert.assertArrayEquals(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9}, result);

2.11 Ints.toByteArray

Returns big-endian representation of int value into byte array of length 4.

int a = 12131415;
byte[] b = Ints.toByteArray(a);

2.12 Ints.fromByteArray

Returns int value from byte array of length 4.

int a = 12131415;
byte[] b = Ints.toByteArray(a);
Assert.assertEquals(a, Ints.fromByteArray(b));

2.13 Ints.fromBytes

Same result as method fromByteArray except it accepts 4 parameters.

int a = 12131415;
byte[] b = Ints.toByteArray(a);
int result = Ints.fromBytes(b[0], b[1], b[2], b[3]);
Assert.assertEquals(12131415, result);

2.14 Ints.stringConverter

Conversion of String to Integer and Integer to String.

Converter<String, Integer> converter = Ints.stringConverter();
int i = converter.convert("1").intValue();
Assert.assertEquals(1, i);

String one = converter.reverse().convert(Integer.valueOf(1));
Assert.assertEquals("1", one);

int j = converter.convert("0111").intValue();
Assert.assertEquals(73, j);

2.15 Ints.join

Accepts the varargs and converts it to String.

String str = Ints.join(", ", 1, 2, 3, 4);
Assert.assertEquals("1, 2, 3, 4", str);

2.16 Ints.lexicographicalComparator

Comparator for int[] array. Comparison is done lexically.

Example : [1] < [1, 2] < [2]
Input:
[1]
[2, 3]
1, 2, 3]

Output:
[1]
[1, 2, 3]
[2, 3]

Comparator<int[]> intArrComparator = Ints.lexicographicalComparator();
int[] a = {1};
int[] b = {2, 3};
int[] c = {1, 2, 3};

List<int[]> list = Arrays.asList(a, b, c);
for (int[] arr : list) {
    System.out.println(Arrays.toString(arr));
}
Collections.sort(list, intArrComparator);
for (int[] arr : list) {
    System.out.println(Arrays.toString(arr));
}

2.17 Ints.toArray

Converts Collection<? extends Number> to int[] using intValue().

List<Integer> integers = Arrays.asList(1, 2, 3, 4, 5);
int[] arr = Ints.toArray(integers);
Assert.assertArrayEquals(new int[]{1, 2, 3, 4, 5}, arr);

2.18 Ints.asList

Converts array to List<Integer>

int[] arr = {1, 2, 3, 4, 5};
List<Integer> list = Ints.asList(arr);
Assert.assertEquals(Arrays.asList(1, 2, 3, 4, 5), list);

2.19 Ints.tryParse

Tries to parse String to Integer. Returns Integer if parsing is successful else returns null.

Integer a = Ints.tryParse("42");
Assert.assertEquals(Integer.valueOf(42), a);

// If long value is passed it returns null
Integer b = Ints.tryParse("87179869184");
Assert.assertTrue(b == null);

2.20 Ints.sortDescending

Sorts the array in descending order.

int[] arr = {1, 2, 3, 4, 5};
Ints.sortDescending(arr);
Assert.assertArrayEquals(new int[]{5, 4, 3, 2, 1}, arr);

2.21 Ints.sortDescending

Sorts the range of array in descending order. inclusive of fromIndex, exclusive of toIndex.

int[] arr = {46, 76, 3, 4, 5, 33};
Ints.sortDescending(arr, 1, arr.length - 1);
Assert.assertArrayEquals(new int[]{46, 76, 5, 4, 3, 33}, arr);

2.22 Ints.reverse

Reverses the array.

int[] arr = {5, 6, 47, 6, 34};
Ints.reverse(arr);
Assert.assertArrayEquals(new int[]{34, 6, 47, 6, 5}, arr);

2.23 Ints.reverse

Reverses the range of array. inclusive of fromIndex, exclusive of toIndex.

int[] arr = {46, 76, 3, 4, 5, 33};
Ints.reverse(arr, 1, arr.length - 1);
Assert.assertArrayEquals(new int[]{46, 5, 4, 3, 76, 33}, arr);

3. Conclusion

In this article, we saw static utility methods of Ints class and how to use them.

Leave a Reply

Your email address will not be published. Required fields are marked *