public final class Ints
extends java.lang.Object
int primitives, that are not
already found in either Integer or Arrays.
See the Guava User Guide article on primitive utilities.
| Modifier and Type | Field and Description |
|---|---|
static int |
BYTES
The number of bytes required to represent a primitive
int
value. |
static int |
MAX_POWER_OF_TWO
The largest power of two that can be represented as an
int. |
| Modifier and Type | Method and Description |
|---|---|
static java.util.List<java.lang.Integer> |
asList(int... backingArray) |
static int |
checkedCast(long value)
Returns the
int value that is equal to value, if possible. |
static int |
compare(int a,
int b) |
static int[] |
concat(int[]... arrays)
Returns the values from each provided array combined into a single array.
|
static boolean |
contains(int[] array,
int target)
Returns
true if target is present as an element anywhere in
array. |
static int[] |
ensureCapacity(int[] array,
int minLength,
int padding) |
static int |
fromByteArray(byte[] bytes) |
static int |
fromBytes(byte b1,
byte b2,
byte b3,
byte b4) |
static int |
hashCode(int value)
Returns a hash code for
value; equal to the result of invoking
((Integer) value).hashCode(). |
static int |
indexOf(int[] array,
int target)
Returns the index of the first appearance of the value
target in
array. |
static int |
indexOf(int[] array,
int[] target) |
static java.lang.String |
join(java.lang.String separator,
int... array) |
static int |
lastIndexOf(int[] array,
int target)
Returns the index of the last appearance of the value
target in
array. |
static java.util.Comparator<int[]> |
lexicographicalComparator() |
static int |
max(int... array)
Returns the greatest value present in
array. |
static int |
min(int... array)
Returns the least value present in
array. |
static int |
saturatedCast(long value)
Returns the
int nearest in value to value. |
static Converter<java.lang.String,java.lang.Integer> |
stringConverter()
Returns a serializable converter object that converts between strings and
integers using
Integer.decode(java.lang.String) and Integer.toString(). |
static int[] |
toArray(java.util.Collection<? extends java.lang.Number> collection) |
static byte[] |
toByteArray(int value) |
static java.lang.Integer |
tryParse(java.lang.String string)
Parses the specified string as a signed decimal integer value.
|
public static final int BYTES
int
value.public static final int MAX_POWER_OF_TWO
int.public static int hashCode(int value)
value; equal to the result of invoking
((Integer) value).hashCode().value - a primitive int valuepublic static int checkedCast(long value)
int value that is equal to value, if possible.value - any value in the range of the int typeint value that equals valuejava.lang.IllegalArgumentException - if value is greater than Integer.MAX_VALUE or less than Integer.MIN_VALUEpublic static int saturatedCast(long value)
int nearest in value to value.value - any long valueint if it is in the range of the
int type, Integer.MAX_VALUE if it is too large,
or Integer.MIN_VALUE if it is too smallpublic static int compare(int a,
int b)
a - the first int to compareb - the second int to comparea is less than b; a positive
value if a is greater than b; or zero if they are equalpublic static boolean contains(int[] array,
int target)
true if target is present as an element anywhere in
array.array - an array of int values, possibly emptytarget - a primitive int valuetrue if array[i] == target for some value of ipublic static int indexOf(int[] array,
int target)
target in
array.array - an array of int values, possibly emptytarget - a primitive int valuei for which array[i] == target, or
-1 if no such index exists.public static int indexOf(int[] array,
int[] target)
array - the array to search for the sequence targettarget - the array to search for as a sub-sequence of arraypublic static int lastIndexOf(int[] array,
int target)
target in
array.array - an array of int values, possibly emptytarget - a primitive int valuei for which array[i] == target,
or -1 if no such index exists.public static int min(int... array)
array.array - a nonempty array of int valuesarray that is less than or equal to
every other value in the arrayjava.lang.IllegalArgumentException - if array is emptypublic static int max(int... array)
array.array - a nonempty array of int valuesarray that is greater than or equal to
every other value in the arrayjava.lang.IllegalArgumentException - if array is emptypublic static int[] concat(int[]... arrays)
concat(new int[] {a, b}, new int[] {}, new
int[] {c} returns the array {a, b, c}.arrays - zero or more int arrayspublic static byte[] toByteArray(int value)
value - valuepublic static int fromByteArray(byte[] bytes)
bytes - bytesjava.lang.IllegalArgumentException - if bytes has fewer than 4 elementspublic static int fromBytes(byte b1,
byte b2,
byte b3,
byte b4)
b1 - b1b2 - b2b3 - b3b4 - b4public static Converter<java.lang.String,java.lang.Integer> stringConverter()
Integer.decode(java.lang.String) and Integer.toString().public static int[] ensureCapacity(int[] array,
int minLength,
int padding)
array - the source arrayminLength - the minimum length the returned array must guaranteepadding - an extra amount to "grow" the array by if growth is
necessaryarray, with guaranteed
minimum length minLengthjava.lang.IllegalArgumentException - if minLength or padding is
negativepublic static java.lang.String join(java.lang.String separator,
int... array)
separator - the text that should appear between consecutive values in
the resulting string (but not at the start or end)array - an array of int values, possibly emptypublic static java.util.Comparator<int[]> lexicographicalComparator()
public static int[] toArray(java.util.Collection<? extends java.lang.Number> collection)
collection - a collection of Number instancescollection, in the
same order, converted to primitivesjava.lang.NullPointerException - if collection or any of its elements
is nullCollection<Integer> before 12.0)public static java.util.List<java.lang.Integer> asList(int... backingArray)
backingArray - the array to back the listpublic static java.lang.Integer tryParse(java.lang.String string)
'-' ('\u002D') is recognized as the
minus sign.
Unlike Integer.parseInt(String), this method returns
null instead of throwing an exception if parsing fails.
Additionally, this method only accepts ASCII digits, and returns
null if non-ASCII digits are present in the string.
Note that strings prefixed with ASCII '+' are rejected, even
under JDK 7, despite the change to Integer.parseInt(String) for
that version.
string - the string representation of an integer valuestring, or null if
string has a length of zero or cannot be parsed as an integer
value