Ints Class | Guava | Java
Last Updated :
28 May, 2018
Ints is a utility class for primitive type int. It provides Static utility methods pertaining to int primitives, that are not already found in either Integer or Arrays.
Declaration :
@GwtCompatible(emulated=true)
public final class Ints
extends Object
Below table shows the Field summary for Guava Ints Class :
Some of the methods provided by Ints Class are :
Exceptions :
- checkedCast : IllegalArgumentException if value is greater than Integer.MAX_VALUE or less than Integer.MIN_VALUE
- min : IllegalArgumentException if array is empty.
- max : IllegalArgumentException if array is empty.
- fromByteArray : IllegalArgumentException if bytes has fewer than 4 elements.
- ensureCapacity : IllegalArgumentException if minLength or padding is negative.
- toArray : NullPointerException if collection or any of its elements is null.
Below table shows some other methods provided by Guava’s Ints Class :
Below given are some examples showing the implementation of methods of Guava’s Ints Class :
Example 1 :
import com.google.common.primitives.Ints;
import java.util.*;
class GFG {
public static void main(String[] args)
{
int arr[] = { 5 , 10 , 15 , 20 , 25 };
List<Integer> myList = Ints.asList(arr);
System.out.println(myList);
}
}
|
Output :
[5, 10, 15, 20, 25]
Example 2 :
import com.google.common.primitives.Ints;
import java.util.*;
class GFG {
public static void main(String[] args)
{
List<Integer> myList = Arrays.asList( 5 , 10 , 15 , 20 , 25 );
int [] arr = Ints.toArray(myList);
System.out.println(Arrays.toString(arr));
}
}
|
Output :
[5, 10, 15, 20, 25]
Example 3 :
import com.google.common.primitives.Ints;
import java.util.*;
class GFG {
public static void main(String[] args)
{
int [] arr1 = { 5 , 10 , 15 };
int [] arr2 = { 20 , 25 };
int [] arr = Ints.concat(arr1, arr2);
System.out.println(Arrays.toString(arr));
}
}
|
Output :
[5, 10, 15, 20, 25]
Example 4 :
import com.google.common.primitives.Ints;
class GFG {
public static void main(String[] args)
{
int [] arr = { 5 , 10 , 15 , 20 };
System.out.println(Ints.contains(arr, 10 ));
System.out.println(Ints.contains(arr, 17 ));
}
}
|
output :
true
false
Example 5 :
import com.google.common.primitives.Ints;
class GFG {
public static void main(String[] args)
{
int [] arr = { 5 , 10 , 15 , 20 };
System.out.println(Ints.min(arr));
}
}
|
Output :
5
Example 6 :
import com.google.common.primitives.Ints;
class GFG {
public static void main(String[] args)
{
int [] arr = { 5 , 10 , 15 , 20 };
System.out.println(Ints.max(arr));
}
}
|
Output :
20
Share your thoughts in the comments
Please Login to comment...