类 Ints

java.lang.Object
com.alibaba.ageiport.common.lang.Ints

public final class Ints extends Object
Static utility methods pertaining to int primitives, that are not already found in either Integer or Arrays.
  • 字段概要

    字段
    修饰符和类型
    字段
    说明
    static final int
    The number of bytes required to represent a primitive int value.
    static final int
    The largest power of two that can be represented as an int.
  • 方法概要

    修饰符和类型
    方法
    说明
    static List<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)
    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[]>
    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.
    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)
     
    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.

    从类继承的方法 java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 字段详细资料

    • BYTES

      public static final int BYTES
      The number of bytes required to represent a primitive int value.

      Java 8 users: use Integer.BYTES instead.

      另请参阅:
    • MAX_POWER_OF_TWO

      public static final int MAX_POWER_OF_TWO
      The largest power of two that can be represented as an int.
      从以下版本开始:
      10.0
      另请参阅:
  • 方法详细资料

    • hashCode

      public static int hashCode(int value)
      Returns a hash code for value; equal to the result of invoking ((Integer) value).hashCode().

      Java 8 users: use Integer.hashCode(int) instead.

      参数:
      value - a primitive int value
      返回:
      a hash code for the value
    • checkedCast

      public static int checkedCast(long value)
      Returns the int value that is equal to value, if possible.
      参数:
      value - any value in the range of the int type
      返回:
      the int value that equals value
      抛出:
      IllegalArgumentException - if value is greater than Integer.MAX_VALUE or less than Integer.MIN_VALUE
    • saturatedCast

      public static int saturatedCast(long value)
      Returns the int nearest in value to value.
      参数:
      value - any long value
      返回:
      the same value cast to int 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 small
    • compare

      public static int compare(int a, int b)
      Compares the two specified 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 compare
      b - the second int to compare
      返回:
      a negative value if a is less than b; a positive value if a is greater than b; or zero if they are equal
    • contains

      public static boolean contains(int[] array, int target)
      Returns true if target is present as an element anywhere in array.
      参数:
      array - an array of int values, possibly empty
      target - a primitive int value
      返回:
      true if array[i] == target for some value of i
    • indexOf

      public static int indexOf(int[] array, int target)
      Returns the index of the first appearance of the value target in array.
      参数:
      array - an array of int values, possibly empty
      target - a primitive int value
      返回:
      the least index i for which array[i] == target, or -1 if no such index exists.
    • indexOf

      public 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.

      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
    • lastIndexOf

      public static int lastIndexOf(int[] array, int target)
      Returns the index of the last appearance of the value target in array.
      参数:
      array - an array of int values, possibly empty
      target - a primitive int value
      返回:
      the greatest index i for which array[i] == target, or -1 if no such index exists.
    • min

      public static int min(int... array)
      Returns the least value present in array.
      参数:
      array - a nonempty array of int values
      返回:
      the value present in array that is less than or equal to every other value in the array
      抛出:
      IllegalArgumentException - if array is empty
    • max

      public static int max(int... array)
      Returns the greatest value present in array.
      参数:
      array - a nonempty array of int values
      返回:
      the value present in array that is greater than or equal to every other value in the array
      抛出:
      IllegalArgumentException - if array is empty
    • constrainToRange

      public static int constrainToRange(int value, int min, int max)
      Returns the value nearest to 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 constrain
      min - the lower bound (inclusive) of the range to constrain value to
      max - the upper bound (inclusive) of the range to constrain value to
      抛出:
      IllegalArgumentException - if min > max
      从以下版本开始:
      21.0
    • concat

      public static int[] concat(int[]... arrays)
      Returns the values from each provided array combined into a single array. For example, concat(new int[] {a, b}, new int[] {}, new int[] {c} returns the array {a, b, c}.
      参数:
      arrays - zero or more int arrays
      返回:
      a single array containing all the values from the source arrays, in order
    • toByteArray

      public static byte[] toByteArray(int value)
    • fromByteArray

      public 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(). 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 elements
    • fromBytes

      public 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}).
      从以下版本开始:
      7.0
    • stringConverter

      public 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(). 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.

      从以下版本开始:
      16.0
    • ensureCapacity

      public 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. 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 array
      minLength - the minimum length the returned array must guarantee
      padding - an extra amount to "grow" the array by if growth is necessary
      返回:
      an array containing the values of array, with guaranteed minimum length minLength
      抛出:
      IllegalArgumentException - if minLength or padding is negative
    • join

      public static String join(String separator, int... array)
      Returns a string containing the supplied 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 empty
    • lexicographicalComparator

      public static Comparator<int[]> lexicographicalComparator()
      Returns a comparator that compares two 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[]).

      从以下版本开始:
      2.0
    • sortDescending

      public static void sortDescending(int[] array)
      Sorts the elements of array in descending order.
      从以下版本开始:
      23.1
    • sortDescending

      public static void sortDescending(int[] array, int fromIndex, int toIndex)
      Sorts the elements of array between fromIndex inclusive and toIndex exclusive in descending order.
      从以下版本开始:
      23.1
    • reverse

      public static void reverse(int[] array)
      Reverses the elements of array. This is equivalent to Collections.reverse(Ints.asList(array)), but is likely to be more efficient.
      从以下版本开始:
      23.1
    • reverse

      public static void reverse(int[] array, int fromIndex, int toIndex)
      Reverses the elements of 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
      从以下版本开始:
      23.1
    • toArray

      public 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().

      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 instances
      返回:
      an array containing the same values as collection, in the same order, converted to primitives
      抛出:
      NullPointerException - if collection or any of its elements is null
      从以下版本开始:
      1.0 (parameter was Collection<Integer> before 12.0)
    • asList

      public static List<Integer> asList(int... backingArray)
    • tryParse

      public static Integer tryParse(String string)
      Parses the specified string as a signed decimal integer value. The ASCII character '-' ('\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 value
      返回:
      the integer value represented by string, or null if string has a length of zero or cannot be parsed as an integer value
      抛出:
      NullPointerException - if string is null
      从以下版本开始:
      11.0
    • tryParse

      public static Integer tryParse(String string, int radix)
      Parses the specified string as a signed integer value using the specified radix. The ASCII character '-' ('\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 value
      radix - the radix to use when parsing
      返回:
      the integer value represented by string using radix, or null if string has a length of zero or cannot be parsed as an integer value
      抛出:
      IllegalArgumentException - if radix < Character.MIN_RADIX or radix > Character.MAX_RADIX
      NullPointerException - if string is null
      从以下版本开始:
      19.0