4.6.5 Java8增强的工具类 Arrays

4.6.5 Java8增强的工具类 Arrays

Java提供的Arrays类里包含的一些static修饰的方法可以直接操作数组,这个Arrays类里包含了如下几个static修饰的方法(static修饰的方法可以直接通过类名调用)。
未完待续…

程序示例

下面程序示范了Arrays类的用法:

Java8增强的Arrays方法

Java8增强了Arrays类的功能,为Arrays类增加了一些工具方法,这些工具方法可以充分利用**多CPU**并行的能力来提高设值、排序的性能。下面是Java8Arrays类增加的工具方法。

binarySearch方法

方法 描述
static int binarySearch(byte[] a, byte key) 使用二分法查询key元素值在a数组中出现的索引;如果a数组不包含key元素值,则返回负数。调用该方法时要求数组中元素已经按升序排列,这样才能得到正确结果
static int binarySearch(byte[] a, int fromIndex, int toIndex, byte key) 这个方法与前一个方法类似,但它只搜索a数组中fromIndextolndex索引的元素。调用该方法时要求数组中元素已经按升序排列,这样才能得到正确结果。
static int binarySearch(char[] a, char key) Searches the specified array of chars for the specified value using the binary search algorithm.
static int binarySearch(char[] a, int fromIndex, int toIndex, char key) Searches a range of the specified array of chars for the specified value using the binary search algorithm.
static int binarySearch(double[] a, double key) Searches the specified array of doubles for the specified value using the binary search algorithm.
static int binarySearch(double[] a, int fromIndex, int toIndex, double key) Searches a range of the specified array of doubles for the specified value using the binary search algorithm.
static int binarySearch(float[] a, float key) Searches the specified array of floats for the specified value using the binary search algorithm.
static int binarySearch(float[] a, int fromIndex, int toIndex, float key) Searches a range of the specified array of floats for the specified value using the binary search algorithm.
static int binarySearch(int[] a, int key) Searches the specified array of ints for the specified value using the binary search algorithm.
static int binarySearch(int[] a, int fromIndex, int toIndex, int key) Searches a range of the specified array of ints for the specified value using the binary search algorithm.
static int binarySearch(long[] a, int fromIndex, int toIndex, long key) Searches a range of the specified array of longs for the specified value using the binary search algorithm.
static int binarySearch(long[] a, long key) Searches the specified array of longs for the specified value using the binary search algorithm.
static int binarySearch(short[] a, int fromIndex, int toIndex, short key) Searches a range of the specified array of shorts for the specified value using the binary search algorithm.
static int binarySearch(short[] a, short key) Searches the specified array of shorts for the specified value using the binary search algorithm.
static int binarySearch(Object[] a, int fromIndex, int toIndex, Object key) Searches a range of the specified array for the specified object using the binary search algorithm.
static int binarySearch(Object[] a, Object key) Searches the specified array for the specified object using the binary search algorithm.
static <T> int binarySearch(T[] a, int fromIndex, int toIndex, T key, Comparator<? super T> c) Searches a range of the specified array for the specified object using the binary search algorithm.
static <T> int binarySearch(T[] a, T key, Comparator<? super T> c) Searches the specified array for the specified object using the binary search algorithm.

copyOf方法

方法 描述
static boolean[] copyOf(boolean[] original, int newLength) 这个方法将会把original数组复制成一个新数组,其中length是新数组的长度。如果length小于original数组的长度,则新数组就是原数组的前面length个元素;如果length大于original数组的长度,则新数组的前面元素就是原数组的所有元素,后面补充0(数值类型)、false(布尔类型)或者null(引用类型)
static byte[] copyOf(byte[] original, int newLength) Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.
static char[] copyOf(char[] original, int newLength) Copies the specified array, truncating or padding with null characters (if necessary) so the copy has the specified length.
static double[] copyOf(double[] original, int newLength) Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.
static float[] copyOf(float[] original, int newLength) Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.
static int[] copyOf(int[] original, int newLength) Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.
static long[] copyOf(long[] original, int newLength) Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.
static short[] copyOf(short[] original, int newLength) Copies the specified array, truncating or padding with zeros (if necessary) so the copy has the specified length.
static <T> T[] copyOf(T[] original, int newLength) Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length.
static <T,U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) Copies the specified array, truncating or padding with nulls (if necessary) so the copy has the specified length.

copyOfRange方法

方法 描述
static boolean[] copyOfRange(boolean[] original, int from, int to) 这个方法与前面方法相似,但这个方法只复制original数组的from索引到to索引的元素。
static byte[] copyOfRange(byte[] original, int from, int to) Copies the specified range of the specified array into a new array.
static char[] copyOfRange(char[] original, int from, int to) Copies the specified range of the specified array into a new array.
static double[] copyOfRange(double[] original, int from, int to) Copies the specified range of the specified array into a new array.
static float[] copyOfRange(float[] original, int from, int to) Copies the specified range of the specified array into a new array.
static int[] copyOfRange(int[] original, int from, int to) Copies the specified range of the specified array into a new array.
static long[] copyOfRange(long[] original, int from, int to) Copies the specified range of the specified array into a new array.
static short[] copyOfRange(short[] original, int from, int to) Copies the specified range of the specified array into a new array.
static <T> T[] copyOfRange(T[] original, int from, int to) Copies the specified range of the specified array into a new array.
static <T,U> T[] copyOfRange(U[] original, int from, int to, Class<? extends T[]> newType) Copies the specified range of the specified array into a new array.

fill方法

方法 描述
static void fill(boolean[] a, boolean val) 该方法将会把a数组的所有元素都赋值为val
static void fill(boolean[] a, int fromIndex, int toIndex, boolean val) 该方法与前一个方法的作用相同,区别只是该方法仅仅将a数组的fromIndextoIndex索引的数组元素赋值为val
static void fill(byte[] a, byte val) Assigns the specified byte value to each element of the specified array of bytes.
static void fill(byte[] a, int fromIndex, int toIndex, byte val) Assigns the specified byte value to each element of the specified range of the specified array of bytes.
static void fill(char[] a, char val) Assigns the specified char value to each element of the specified array of chars.
static void fill(char[] a, int fromIndex, int toIndex, char val) Assigns the specified char value to each element of the specified range of the specified array of chars.
static void fill(double[] a, double val) Assigns the specified double value to each element of the specified array of doubles.
static void fill(double[] a, int fromIndex, int toIndex, double val) Assigns the specified double value to each element of the specified range of the specified array of doubles.
static void fill(float[] a, float val) Assigns the specified float value to each element of the specified array of floats.
static void fill(float[] a, int fromIndex, int toIndex, float val) Assigns the specified float value to each element of the specified range of the specified array of floats.
static void fill(int[] a, int val) Assigns the specified int value to each element of the specified array of ints.
static void fill(int[] a, int fromIndex, int toIndex, int val) Assigns the specified int value to each element of the specified range of the specified array of ints.
static void fill(long[] a, int fromIndex, int toIndex, long val) Assigns the specified long value to each element of the specified range of the specified array of longs.
static void fill(long[] a, long val) Assigns the specified long value to each element of the specified array of longs.
static void fill(short[] a, int fromIndex, int toIndex, short val) Assigns the specified short value to each element of the specified range of the specified array of shorts.
static void fill(short[] a, short val) Assigns the specified short value to each element of the specified array of shorts.
static void fill(Object[] a, int fromIndex, int toIndex, Object val) Assigns the specified Object reference to each element of the specified range of the specified array of Objects.
static void fill(Object[] a, Object val) Assigns the specified Object reference to each element of the specified array of Objects.

sort方法

方法 描述
static void sort(byte[] a) 该方法对a数组的数组元素进行排序
static void sort(byte[] a, int fromIndex, int toIndex) 该方法与前一个方法相似,区别是该方法仅仅对fromIndextoIndex索引的元素进行排序。
static void sort(char[] a) Sorts the specified array into ascending numerical order.
static void sort(char[] a, int fromIndex, int toIndex) Sorts the specified range of the array into ascending order.
static void sort(double[] a) Sorts the specified array into ascending numerical order.
static void sort(double[] a, int fromIndex, int toIndex) Sorts the specified range of the array into ascending order.
static void sort(float[] a) Sorts the specified array into ascending numerical order.
static void sort(float[] a, int fromIndex, int toIndex) Sorts the specified range of the array into ascending order.
static void sort(int[] a) Sorts the specified array into ascending numerical order.
static void sort(int[] a, int fromIndex, int toIndex) Sorts the specified range of the array into ascending order.
static void sort(long[] a) Sorts the specified array into ascending numerical order.
static void sort(long[] a, int fromIndex, int toIndex) Sorts the specified range of the array into ascending order.
static void sort(short[] a) Sorts the specified array into ascending numerical order.
static void sort(short[] a, int fromIndex, int toIndex) Sorts the specified range of the array into ascending order.
static void sort(Object[] a) Sorts the specified array of objects into ascending order, according to the natural ordering of its elements.
static void sort(Object[] a, int fromIndex, int toIndex) Sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements.
static <T> void sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c) Sorts the specified range of the specified array of objects according to the order induced by the specified comparator.
static <T> void sort(T[] a, Comparator<? super T> c) Sorts the specified array of objects according to the order induced by the specified comparator.

equals方法

方法 描述
static boolean equals(boolean[] a, boolean[] a2) 如果a数组和a2数组的长度相等,而且a数组和a2数组的数组元素也一一相同,该方法将返回true
static boolean equals(boolean[] a, int aFromIndex, int aToIndex, boolean[] b, int bFromIndex, int bToIndex) Returns true if the two specified arrays of booleans, over the specified ranges, are equal to one another.
static boolean equals(byte[] a, byte[] a2) Returns true if the two specified arrays of bytes are equal to one another.
static boolean equals(byte[] a, int aFromIndex, int aToIndex, byte[] b, int bFromIndex, int bToIndex) Returns true if the two specified arrays of bytes, over the specified ranges, are equal to one another.
static boolean equals(char[] a, char[] a2) Returns true if the two specified arrays of chars are equal to one another.
static boolean equals(char[] a, int aFromIndex, int aToIndex, char[] b, int bFromIndex, int bToIndex) Returns true if the two specified arrays of chars, over the specified ranges, are equal to one another.
static boolean equals(double[] a, double[] a2) Returns true if the two specified arrays of doubles are equal to one another.
static boolean equals(double[] a, int aFromIndex, int aToIndex, double[] b, int bFromIndex, int bToIndex) Returns true if the two specified arrays of doubles, over the specified ranges, are equal to one another.
static boolean equals(float[] a, float[] a2) Returns true if the two specified arrays of floats are equal to one another.
static boolean equals(float[] a, int aFromIndex, int aToIndex, float[] b, int bFromIndex, int bToIndex) Returns true if the two specified arrays of floats, over the specified ranges, are equal to one another.
static boolean equals(int[] a, int[] a2) Returns true if the two specified arrays of ints are equal to one another.
static boolean equals(int[] a, int aFromIndex, int aToIndex, int[] b, int bFromIndex, int bToIndex) Returns true if the two specified arrays of ints, over the specified ranges, are equal to one another.
static boolean equals(long[] a, int aFromIndex, int aToIndex, long[] b, int bFromIndex, int bToIndex) Returns true if the two specified arrays of longs, over the specified ranges, are equal to one another.
static boolean equals(long[] a, long[] a2) Returns true if the two specified arrays of longs are equal to one another.
static boolean equals(short[] a, int aFromIndex, int aToIndex, short[] b, int bFromIndex, int bToIndex) Returns true if the two specified arrays of shorts, over the specified ranges, are equal to one another.
static boolean equals(short[] a, short[] a2) Returns true if the two specified arrays of shorts are equal to one another.
static boolean equals(Object[] a, int aFromIndex, int aToIndex, Object[] b, int bFromIndex, int bToIndex) Returns true if the two specified arrays of Objects, over the specified ranges, are equal to one another.
static boolean equals(Object[] a, Object[] a2) Returns true if the two specified arrays of Objects are equal to one another.
static <T> boolean equals(T[] a, int aFromIndex, int aToIndex, T[] b, int bFromIndex, int bToIndex, Comparator<? super T> cmp) Returns true if the two specified arrays of Objects, over the specified ranges, are equal to one another.
static <T> boolean equals(T[] a, T[] a2, Comparator<? super T> cmp) Returns true if the two specified arrays of Objects are equal to one another.

toString方法

方法 描述
static String toString(boolean[] a) 该方法将一个数组转换成一个字符串。该方法按顺序把多个数组元素连缀在一起,多个数组元素使用英文逗号(,)和空格隔开。
static String toString(byte[] a) Returns a string representation of the contents of the specified array.
static String toString(char[] a) Returns a string representation of the contents of the specified array.
static String toString(double[] a) Returns a string representation of the contents of the specified array.
static String toString(float[] a) Returns a string representation of the contents of the specified array.
static String toString(int[] a) Returns a string representation of the contents of the specified array.
static String toString(long[] a) Returns a string representation of the contents of the specified array.
static String toString(short[] a) Returns a string representation of the contents of the specified array.
static String toString(Object[] a) Returns a string representation of the contents of the specified array.

程序示例 Arrays类Java8新增方法

下面程序示范了Java8Arrays类新增的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.util.Arrays;

public class ArraysTest {
public static void main(String[] args) {
// 定义一个a数组
int[] a = new int[] { 3, 4, 5, 6 };
// 定义一个a2数组
int[] a2 = new int[] { 3, 4, 5, 6 };
// a数组和a2数组的长度相等,每个元素依次相等,将输出true
System.out.println("a数组和a2数组是否相等:" + Arrays.equals(a, a2));
// 通过复制a数组,生成一个新的b数组
int[] b = Arrays.copyOf(a, 6);
System.out.println("a数组和b数组是否相等:" + Arrays.equals(a, b));
// 输出b数组的元素,将输出[3, 4, 5, 6, 0, 0]
System.out.println("b数组的元素为:" + Arrays.toString(b));
// 将b数组的第3个元素(包括)到第5个元素(不包括)赋为1
Arrays.fill(b, 2, 4, 1);
// 输出b数组的元素,将输出[3, 4, 1, 1, 0, 0]
System.out.println("b数组的元素为:" + Arrays.toString(b));
// 对b数组进行排序
Arrays.sort(b);
// 输出b数组的元素,将输出[0, 0, 1, 1, 3, 4]
System.out.println("b数组的元素为:" + Arrays.toString(b));
}
}

System类的arrayCopy复制数组

除此之外,在System类里也包含了一个arrayCopy方法,该方法可以将src数组里的元素值赋给dest数组的元素,其中srcPos指定从src数组的第几个元素开始赋值,length参数指定将src数组的多少个元素值赋给dest数组的元素。

方法 描述
static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length) Copies an array from the specified source array, beginning at the specified position, to the specified position of the destination array.

java8新增方法

Java8增强了Arrays类的功能,为Arrays类增加了一些工具方法,这些工具方法可以充分利用多CPU并行的能力来提高设值、排序的性能。下面是Java8Arrays类增加的工具方法。
下面的方法中,所有parallel开头的方法都表示该方法可利用CPU并行的能力来提高性能

parallelPrefix方法

方法 描述
static void parallelPrefix(double[] array, DoubleBinaryOperator op) 该方法使用op参数指定的计算公式计算得到的结果作为新的元素。op计算公式包括letright两个形参,其中left代表数组中前一个索引处的元素,ight代表数组中当前索引处的元素,当计算第一个新数组元素时,left的值默认为1
static void parallelPrefix(double[] array, int fromIndex, int toIndex, DoubleBinaryOperator op) 该方法与上一个方法相似,区别是该方法仅重新计算fromIndextolndex索引的元素。
static void parallelPrefix(int[] array, int fromIndex, int toIndex, IntBinaryOperator op) Performs parallelPrefix(int[], IntBinaryOperator) for the given subrange of the array.
static void parallelPrefix(int[] array, IntBinaryOperator op) Cumulates, in parallel, each element of the given array in place, using the supplied function.
static void parallelPrefix(long[] array, int fromIndex, int toIndex, LongBinaryOperator op) Performs parallelPrefix(long[], LongBinaryOperator) for the given subrange of the array.
static void parallelPrefix(long[] array, LongBinaryOperator op) Cumulates, in parallel, each element of the given array in place, using the supplied function.
static <T> void parallelPrefix(T[] array, int fromIndex, int toIndex, BinaryOperator<T> op) Performs parallelPrefix(Object[], BinaryOperator) for the given subrange of the array.
static <T> void parallelPrefix(T[] array, BinaryOperator<T> op) Cumulates, in parallel, each element of the given array in place, using the supplied function.

setAll方法

方法 描述
static void setAll(double[] array, IntToDoubleFunction generator) 该方法使用指定的生成器(generator)为所有数组元素设置值,该生成器控制数组元素的值的生成算法。
static void setAll(long[] array, IntToLongFunction generator) Set all elements of the specified array, using the provided generator function to compute each element.
static void setAll(int[] array, IntUnaryOperator generator) Set all elements of the specified array, using the provided generator function to compute each element.
static <T> void setAll(T[] array, IntFunction<? extends T> generator) Set all elements of the specified array, using the provided generator function to compute each element.

parallelSetAll方法

方法 描述
static void parallelSetAll(double[] array, IntToDoubleFunction generator) 该方法的功能与上一个方法相同,只是该方法增加了并行能力,可以利用多CPU并行来提高性能
static void parallelSetAll(int[] array, IntUnaryOperator generator) Set all elements of the specified array, in parallel, using the provided generator function to compute each element.
static void parallelSetAll(long[] array, IntToLongFunction generator) Set all elements of the specified array, in parallel, using the provided generator function to compute each element.
static <T> void parallelSetAll(T[] array, IntFunction<? extends T> generator) Set all elements of the specified array, in parallel, using the provided generator function to compute each element.

parallelSort方法

方法 描述
static void parallelSort(byte[] a)该方法的功能与Arrays类以前就有的sort()方法相似,只是该方法增加了并行能力,可以利用多CPU并行来提高性能。
static void parallelSort(byte[] a, int fromIndex, int toIndex) 该方法与上一个方法相似,区别是该方法仅加了并行能力,可以利用多CPU并行来提高性能。
static void parallelSort(char[] a) Sorts the specified array into ascending numerical order.
static void parallelSort(char[] a, int fromIndex, int toIndex) Sorts the specified range of the array into ascending numerical order.
static void parallelSort(double[] a) Sorts the specified array into ascending numerical order.
static void parallelSort(double[] a, int fromIndex, int toIndex) Sorts the specified range of the array into ascending numerical order.
static void parallelSort(float[] a) Sorts the specified array into ascending numerical order.
static void parallelSort(float[] a, int fromIndex, int toIndex) Sorts the specified range of the array into ascending numerical order.
static void parallelSort(int[] a) Sorts the specified array into ascending numerical order.
static void parallelSort(int[] a, int fromIndex, int toIndex) Sorts the specified range of the array into ascending numerical order.
static void parallelSort(long[] a) Sorts the specified array into ascending numerical order.
static void parallelSort(long[] a, int fromIndex, int toIndex) Sorts the specified range of the array into ascending numerical order.
static void parallelSort(short[] a) Sorts the specified array into ascending numerical order.
static void parallelSort(short[] a, int fromIndex, int toIndex) Sorts the specified range of the array into ascending numerical order.
static <T extends Comparable<? super T>> void parallelSort(T[] a)
Sorts the specified array of objects into ascending order, according to the natural ordering of its elements. static <T extends Comparable<? super T>>
void parallelSort(T[] a, int fromIndex, int toIndex) Sorts the specified range of the specified array of objects into ascending order, according to the natural ordering of its elements.
static <T> void parallelSort(T[] a, int fromIndex, int toIndex, Comparator<? super T> cmp) Sorts the specified range of the specified array of objects according to the order induced by the specified comparator.
static <T> void parallelSort(T[] a, Comparator<? super T> cmp) Sorts the specified array of objects according to the order induced by the specified comparator.

spliterator方法

方法 描述
static Spliterator.OfDouble spliterator(double[] array) 将该数组的所有元素转换成对应的Spliterator对象。
static Spliterator.OfDouble spliterator(double[] array, int startInclusive, int endExclusive) 该方法与上一个方法相似,区别是该方法仅转换startInclusiveendExclusive索引的元素。
static Spliterator.OfInt spliterator(int[] array) Returns a Spliterator.OfInt covering all of the specified array.
static Spliterator.OfInt spliterator(int[] array, int startInclusive, int endExclusive) Returns a Spliterator.OfInt covering the specified range of the specified array.
static Spliterator.OfLong spliterator(long[] array) Returns a Spliterator.OfLong covering all of the specified array.
static Spliterator.OfLong spliterator(long[] array, int startInclusive, int endExclusive) Returns a Spliterator.OfLong covering the specified range of the specified array.
static <T> Spliterator<T> spliterator(T[] array) Returns a Spliterator covering all of the specified array.
static <T> Spliterator<T> spliterator(T[] array, int startInclusive, int endExclusive) Returns a Spliterator covering the specified range of the specified array.

stream方法

方法 描述
static DoubleStream stream(double[] array) 该方法将数组转换为Stream,StreamJava8新增的流式编程的API
static DoubleStream stream(double[] array, int startInclusive, int endExclusive) 该方法与上一个方法相似,区别是该方法仅将fromIndextoIndex索引的元素转换为Stream
static IntStream stream(int[] array) Returns a sequential IntStream with the specified array as its source.
static IntStream stream(int[] array, int startInclusive, int endExclusive) Returns a sequential IntStream with the specified range of the specified array as its source.
static LongStream stream(long[] array) Returns a sequential LongStream with the specified array as its source.
static LongStream stream(long[] array, int startInclusive, int endExclusive) Returns a sequential LongStream with the specified range of the specified array as its source.
static <T> Stream<T> stream(T[] array) Returns a sequential Stream with the specified array as its source.
static <T> Stream<T> stream(T[] array, int startInclusive, int endExclusive) Returns a sequential Stream with the specified range of the specified array as its source.

程序示例 Arrays类java8新增方法

下面程序示范了Java8Arrays类新增的方法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.Arrays;
import java.util.function.*;

public class ArraysTest2 {
public static void main(String[] args) {
int[] arr1 = new int[] { 3, -4, 25, 16, 30, 18 };
// 对数组arr1进行并发排序
Arrays.parallelSort(arr1);
System.out.println("arr1="+Arrays.toString(arr1));


int[] arr2 = new int[] { 3, -4, 25, 16, 30, 18 };
Arrays.parallelPrefix(arr2, new IntBinaryOperator() {
// left代表数组中前一个所索引处的元素,计算第一个元素时,left为1
// right代表数组中当前索引处的元素
public int applyAsInt(int left, int right) {
return left * right;
}
});
System.out.println("arr2="+Arrays.toString(arr2));


int[] arr3 = new int[5];
Arrays.parallelSetAll(arr3, new IntUnaryOperator() {
// operand代表正在计算的元素索引
public int applyAsInt(int operand) {
return operand * 5;
}
});
System.out.println("arr3="+Arrays.toString(arr3));
}
}

上面程序中调用了parallelSort()方法对数组执行排序,该方法的功能与传统sort()方法大致相似,只是在多CPU机器上会有更好的性能。

第二段代码使用的计算公式为left*right,其中left代表数组中前一个索引处的元素,right代表数组中当前索引处的元素。程序使用的数组为

1
[-4, 3, 16, 18, 25, 30]

计算新的数组元素的方式为:

1
[1*3=3,3*-4=-12,-12*25=-300,-300*16=-48000,-48000*30=-144000,-144000*18=-2592000]

因此将会得到如下新的数组元素:

1
[3, -12, -300, -4800, -144000, -2592000]

第三段代码使用公式operand*5来设置数组元素,该公式中operand代表正在计算的数组元素的索引。因此第三段粗体字代码计算得到的数组为:

1
[0, 5, 10, 15, 20]