The com.google.common.primitives package by Guava provides utility classes for all primitives types such as Ints, Longs, Chars, Doubles, etc. This article will talk about the Ints class by Guava.

The Guava Ints class provides static utility methods to work with primitive int values in Java, which are not already present in Java’s Integer or Arrays classes, such as conversion between arrays and lists, finding the minimum or maximum value, checking if an element is present, and more. Some of the methods are:

1. Using Ints.asList() method

The Guava Ints.asList() method wraps the specified primitive integer array as a list of the Integer type, unlike Arrays.asList(), which takes a non-primitive array. Here is an example of using the Guava’s Ints.asList() method:

Download Code

2. Using Ints.toArray() method

The Guava Ints.toArray() method can be used to convert a list (or set) of Integer to a primitive integer array. Here is an example of using the Guava’s Ints.toArray() method:

Download Code

3. Using Ints.concat() method

The Guava Ints.concat() method combines elements from specified arrays into a single array. Here is a sample code that demonstrates the Guava’s Ints.concat() method:

Download Code

4. Using Ints.contains() method

The Guava Ints.contains() method search for an element in the array and returns true if the element is found, or return false otherwise. Here is a sample code that demonstrates the Guava’s Ints.contains() method:

Download Code

5. Using Ints.indexOf() and Ints.lastIndexOf() method

The Guava Ints.indexOf() method returns the index of the first appearance of the given element in an array and returns -1 if the element is not found in the array. The Ints.lastIndexOf() method works similarly but returns the index of the last appearance. We can use these methods like this:

Download Code

6. Using Ints.join() method

The Guava Ints.join() method returns a string containing the array elements, separated by a separator. The following example demonstrates the usage of the Guava’s Ints.join() method:

Download Code

7. Using Ints.min() and Ints.max() method

The Guava Ints.min() method returns the minimum value present in array and the Ints.max() method returns the maximum value. The following example demonstrates the usage of the Guava’s Ints.min() and Ints.max() method:

Download Code

That’s all about Ints class by Guava in Java.