@GwtCompatible public final class Ints extends 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 List<Integer> |
asList(int... backingArray)
Returns a fixed-size list backed by the specified array, similar to
Arrays.asList(Object[]) . |
static int |
checkedCast(long value)
Returns the
int value that is equal to value , if possible. |
static int |
compare(int a,
int b)
Compares the two specified
int values. |
static int[] |
concat(int[]... arrays)
Returns the values from each provided array combined into a single array.
|
static int |
constrainToRange(int value,
int min,
int max)
Returns the value nearest to
value which is within the closed range [min..max] . |
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)
Returns an array containing the same values as
array , but guaranteed to be of a
specified minimum length. |
static int |
fromByteArray(byte[] bytes)
Returns the
int value whose big-endian representation is stored in the first 4 bytes of
bytes ; equivalent to ByteBuffer.wrap(bytes).getInt() . |
static int |
fromBytes(byte b1,
byte b2,
byte b3,
byte b4)
Returns the
int value whose byte representation is the given 4 bytes, in big-endian
order; equivalent to Ints.fromByteArray(new byte[] {b1, b2, b3, 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)
Returns the start position of the first occurrence of the specified
target within
array , or -1 if there is no such occurrence. |
static String |
join(String separator,
int... array)
Returns a string containing the supplied
int values separated by separator . |
static int |
lastIndexOf(int[] array,
int target)
Returns the index of the last appearance of the value
target in array . |
static Comparator<int[]> |
lexicographicalComparator()
Returns a comparator that compares two
int arrays lexicographically. |
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 void |
reverse(int[] array)
Reverses the elements of
array . |
static void |
reverse(int[] array,
int fromIndex,
int toIndex)
Reverses the elements of
array between fromIndex inclusive and toIndex
exclusive. |
static int |
saturatedCast(long value)
Returns the
int nearest in value to value . |
static void |
sortDescending(int[] array)
Sorts the elements of
array in descending order. |
static void |
sortDescending(int[] array,
int fromIndex,
int toIndex)
Sorts the elements of
array between fromIndex inclusive and toIndex
exclusive in descending order. |
static Converter<String,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(Collection<? extends Number> collection)
Returns an array containing each value of
collection , converted to a int value
in the manner of Number.intValue() . |
static byte[] |
toByteArray(int value)
Returns a big-endian representation of
value in a 4-element byte array; equivalent to
ByteBuffer.allocate(4).putInt(value).array() . |
static Integer |
tryParse(String string)
Parses the specified string as a signed decimal integer value.
|
static Integer |
tryParse(String string,
int radix)
Parses the specified string as a signed integer value using the specified radix.
|
public static final int BYTES
int
value.
Java 8 users: use Integer.BYTES
instead.
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()
.
Java 8 users: use Integer.hashCode(int)
instead.
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 value
IllegalArgumentException
- if value
is greater than Integer.MAX_VALUE
or
less than Integer.MIN_VALUE
public 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)
int
values. The sign of the value returned is the same as
that of ((Integer) a).compareTo(b)
.
Note for Java 7 and later: this method should be treated as deprecated; use the
equivalent Integer.compare(int, int)
method instead.
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 i
public 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)
target
within
array
, or -1
if there is no such occurrence.
More formally, returns the lowest index i
such that Arrays.copyOfRange(array,
i, i + target.length)
contains exactly the same elements as target
.
array
- the array to search for the sequence target
target
- the array to search for as a sub-sequence of array
public 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 arrayIllegalArgumentException
- 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 arrayIllegalArgumentException
- if array
is empty@Beta public static int constrainToRange(int value, int min, int max)
value
which is within the closed range [min..max]
.
If value
is within the range [min..max]
, value
is returned
unchanged. If value
is less than min
, min
is returned, and if value
is greater than max
, max
is returned.
value
- the int
value to constrainmin
- the lower bound (inclusive) of the range to constrain value
tomax
- the upper bound (inclusive) of the range to constrain value
toIllegalArgumentException
- if min > max
public 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
in a 4-element byte array; equivalent to
ByteBuffer.allocate(4).putInt(value).array()
. For example, the input value 0x12131415
would yield the byte array {0x12, 0x13, 0x14, 0x15}
.
If you need to convert and concatenate several values (possibly even of different types),
use a shared ByteBuffer
instance, or use ByteStreams.newDataOutput()
to get a growable buffer.
public static int fromByteArray(byte[] bytes)
int
value whose big-endian representation is stored in the first 4 bytes of
bytes
; equivalent to ByteBuffer.wrap(bytes).getInt()
. For example, the input
byte array {0x12, 0x13, 0x14, 0x15, 0x33}
would yield the int
value 0x12131415
.
Arguably, it's preferable to use ByteBuffer
; that library exposes much more
flexibility at little cost in readability.
IllegalArgumentException
- if bytes
has fewer than 4 elementspublic static int fromBytes(byte b1, byte b2, byte b3, byte b4)
int
value whose byte representation is the given 4 bytes, in big-endian
order; equivalent to Ints.fromByteArray(new byte[] {b1, b2, b3, b4})
.@Beta public static Converter<String,Integer> stringConverter()
Integer.decode(java.lang.String)
and Integer.toString()
. The returned converter throws NumberFormatException
if the input string is invalid.
Warning: please see Integer.decode(java.lang.String)
to understand exactly how strings are
parsed. For example, the string "0123"
is treated as octal and converted to the
value 83
.
public static int[] ensureCapacity(int[] array, int minLength, int padding)
array
, but guaranteed to be of a
specified minimum length. If array
already has a length of at least minLength
,
it is returned directly. Otherwise, a new array of size minLength + padding
is
returned, containing the values of array
, and zeroes in the remaining places.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 minLength
IllegalArgumentException
- if minLength
or padding
is negativepublic static String join(String separator, int... array)
int
values separated by separator
. For
example, join("-", 1, 2, 3)
returns the string "1-2-3"
.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 Comparator<int[]> lexicographicalComparator()
int
arrays lexicographically. That is, it
compares, using compare(int, int)
), the first pair of values that follow any common
prefix, or when one array is a prefix of the other, treats the shorter array as the lesser. For
example, [] < [1] < [1, 2] < [2]
.
The returned comparator is inconsistent with Object.equals(Object)
(since arrays
support only identity equality), but it is consistent with Arrays.equals(int[], int[])
.
public static void sortDescending(int[] array)
array
in descending order.public static void sortDescending(int[] array, int fromIndex, int toIndex)
array
between fromIndex
inclusive and toIndex
exclusive in descending order.public static void reverse(int[] array)
array
. This is equivalent to Collections.reverse(Ints.asList(array))
, but is likely to be more efficient.public static void reverse(int[] array, int fromIndex, int toIndex)
array
between fromIndex
inclusive and toIndex
exclusive. This is equivalent to Collections.reverse(Ints.asList(array).subList(fromIndex, toIndex))
, but is likely to be more
efficient.IndexOutOfBoundsException
- if fromIndex < 0
, toIndex > array.length
, or
toIndex > fromIndex
public static int[] toArray(Collection<? extends Number> collection)
collection
, converted to a int
value
in the manner of Number.intValue()
.
Elements are copied from the argument collection as if by collection.toArray()
.
Calling this method is as thread-safe as calling that method.
collection
- a collection of Number
instancescollection
, in the same order, converted
to primitivesNullPointerException
- if collection
or any of its elements is nullCollection<Integer>
before 12.0)public static List<Integer> asList(int... backingArray)
Arrays.asList(Object[])
. The list supports List.set(int, Object)
, but any attempt to
set a value to null
will result in a NullPointerException
.
The returned list maintains the values, but not the identities, of Integer
objects
written to or read from it. For example, whether list.get(0) == list.get(0)
is true for
the returned list is unspecified.
Note: when possible, you should represent your data as an ImmutableIntArray
instead, which has an asList
view.
backingArray
- the array to back the list@Beta @NullableDecl public static Integer tryParse(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 valueNullPointerException
- if string
is null
@Beta @NullableDecl public static Integer tryParse(String string, int radix)
'-'
('\u002D'
) is recognized as the minus sign.
Unlike Integer.parseInt(String, int)
, 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, int)
for that version.
string
- the string representation of an integer valueradix
- the radix to use when parsingstring
using radix
, or null
if
string
has a length of zero or cannot be parsed as an integer valueIllegalArgumentException
- if radix < Character.MIN_RADIX
or radix >
Character.MAX_RADIX
NullPointerException
- if string
is null
Copyright © 2010–2019. All rights reserved.