Guava’s Ints Class in Java for primitive int array

Guava com.google.common.primitives package provides utility classes for all primitives types such as Ints, Longs, Chars, Doubles, etc. In this article, we will talk about Guava Ints class which provides static utility methods to work with primitive int, which are not present in Java’s Integer or Arrays class.

 

1. Ints.asList()

Guava Ints.asList() method wraps the specified primitive integer array as a List of the Integer type, unlike Arrays.asList() which returns List<int[]>.

 

Download   Run Code

Output:

[1, 2, 3, 4, 5]

 

2. Ints.toArray()

Guava’s Ints.toArray() method can be used to convert a List (or Set) of Integer to an array of int.

 

Download   Run Code

Output:

[1, 2, 3, 4, 5]

 

3. Ints.concat()

Guava Ints.concat() method combines elements from specified arrays into a single array.

 

Download   Run Code

Output:

[1, 2, 3, 4, 5, 6, 7, 8]

 

4. Ints.contains()

Guava Ints.contains() method search for an element in the array and returns true if element is found, or return false otherwise.

 

Download   Run Code

 

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

Guava Ints.indexOf() method returns the index of the first appearance of a given element in array and returns -1 if element is not found in the array. Ints.lastIndexOf() method works in similar way but returns index of the last appearance.

 

Download   Run Code

 

6. Ints.join()

Guava Ints.join() method returns a string containing the elements of array, separated by separator.

 

Download   Run Code

Output:

1,2,3,4,5

 

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

Guava Ints.min() method returns the minimum value present in array and the Ints.max() method returns the maximum value.

 

Download   Run Code

Output:

Minimum value is 1
Maximum value is 5

 

 
Thanks for reading.

Please use our online compiler to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 



Leave a Reply

avatar
  Subscribe  
Notify of