import java.util.Arrays;
stream
image.png
使用效果
Double[] stringArray = {1D, 2D, 3D, 11D, 22D, 33D, 111D, 222D, 333D};
Arrays.stream(stringArray).filter(a -> a > 2D).forEach(aDouble -> System.out.print(aDouble + " 、"));
Arrays.stream(stringArray, 0, 4).filter(a -> a > 2D).forEach(aDouble -> System.out.print(aDouble + " 、"));
3.0 、11.0 、22.0 、33.0 、111.0 、222.0 、333.0 、
3.0 、11.0 、
setAll
image.png
使用效果
double[] a = {1.0, 2.0, 3.0, 4.4};
Arrays.setAll(a, x -> a[x] * a[x]);
System.out.println("数组中整数的平方:" + Arrays.toString(a));
数组中整数的平方:[1.0, 4.0, 9.0, 19.360000000000003]
toString
image.png
- byte、 short、 int、 long、 float、 double、 boolean 、char
- Object
使用效果
String[] s1 = {"wyy", "wzz", "wxx", "wxx"};
System.out.println(Arrays.toString(s1));
[wyy, wzz, wxx, wxx]
binarySearch
image.png
- byte、 short、 int、 long、 float、 double、char
- Object、T(泛型)
左闭右开
重构方法之间的联系如下
public static int binarySearch(byte[] a, byte key) {
return binarySearch0(a, 0, a.length, key);
}
使用效果
Integer[] array1 = {1, 1, 1, 2, 7, 11, 22, 37};
int i = Arrays.binarySearch(array1, 2);
System.out.println(i);
3
sort(默认升序)
image.png
- byte、 short、 int、 long、 float、 double、 char
使用的是DualPivotQuicksort.sort排序法
秉持左闭右开的下标范围 - Object、 T(泛型)
使用的是legacyMergeSort、或者ComparableTimSort.sort、由sun自己配置文件指定
使用效果
Double[] doubleArr = {11D, 22D, 33D, 1D, 2D, 3D, 111D, 222D, 333D};
Arrays.sort(doubleArr);
Arrays.stream(doubleArr).forEach(aDouble -> System.out.print(aDouble + " 、"));
Integer[] intArr = {11, 22, 33, 1, 2, 3, 111, 222, 333};
Arrays.sort(intArr);
Arrays.stream(intArr).forEach(aDouble -> System.out.print(aDouble + " 、"));
1.0 、2.0 、3.0 、11.0 、22.0 、33.0 、111.0 、222.0 、333.0 、
1 、2 、3 、11 、22 、33 、111 、222 、333 、
mismatch
image.png
- byte、 short、 int、 long、 float、 double、 boolean 、char
- Object、 T(泛型)
下标范围是左闭右开
使用效果
int[] array1 = {2, 7, 11, 22, 37};
int[] array2 = {2, 7, 11, 22, 37};
int[] array3 = {2, 7, 19, 31, 39, 56};
int index1 = Arrays.mismatch(array1, array2);
System.out.println(index1);
int index2 = Arrays.mismatch(array1, array3);
System.out.println(index2);
-1
2
int[] array1 = {1, 1, 1, 2, 7, 11, 22, 37};
int[] array2 = {1, 1, 1, 2, 7, 11, 22, 37};
int[] array3 = {1, 1, 1, 2, 7, 19, 22, 39, 56};
int index1 = Arrays.mismatch(array1, 2, 5, array2, 2, 5);
System.out.println(index1);
int index2 = Arrays.mismatch(array1, 2, 5, array3, 2, 5);
System.out.println(index2);
int index3 = Arrays.mismatch(array1, 2, 6, array2, 2, 6);
System.out.println(index3);
int index4 = Arrays.mismatch(array1, 2, 6, array3, 2, 6);
System.out.println(index4);
-1
-1
-1
3
compare
image.png
- byte、 short、 int、 long、 float、 double、boolean、char
- T(泛型)
左闭右开
使用效果: 注意Integer走的是T[]而非int[]
Integer[] array1 = {1, 1, 1, 2, 7, 11, 22, 37};
Integer[] array2 = {1, 1, 1, 2, 7, 11, 22, 37};
int compare = Arrays.compare(array1, array2);
System.out.println(compare);
0
Integer[] array1 = {1, 1, 1, 2, 7, 11, 22};
Integer[] array2 = {1, 1, 1, 2, 7, 11, 22, 37};
int compare = Arrays.compare(array1, array2);
System.out.println(compare);
-1
Integer[] array1 = {1, 1, 1, 2, 7, 11, 22};
Integer[] array2 = {1, 1, 1, 2, 7, 11};
int compare = Arrays.compare(array1, array2);
System.out.println(compare);
1
Integer[] array1 = {1, 1, 1, 2, 7, 11};
Integer[] array2 = {1, 1, 1, 2, 7, 12};
int compare = Arrays.compare(array1, array2);
System.out.println(compare);
-1
Integer[] array1 = {1, 1, 1, 2, 7, 13};
Integer[] array2 = {1, 1, 1, 2, 7, 12};
int compare = Arrays.compare(array1, array2);
System.out.println(compare);
1
Integer[] array1 = {1, 1, 1, 2, 7, -13};
Integer[] array2 = {1, 1, 1, 2, 7, 12};
int compare = Arrays.compare(array1, array2);
System.out.println(compare);
-1
Integer[] array1 = {1, 1, 1, 2, 7, -3};
Integer[] array2 = {1, 1, 1, 2, 7, -12};
int compare = Arrays.compare(array1, array2);
System.out.println(compare);
1
compareUnsigned (待定研究)
hashCode
image.png
- byte、 short、 int、 long、 float、 double、 boolean 、char
- Object
使用效果
int[] array1 = {1, 1, 1, 2, 7, 11, 22, 37};
int[] array2 = {1, 1, 1, 2, 7, 11, 22, 37};
int i = Arrays.hashCode(array1);
int j = Arrays.hashCode(array2);
System.out.println(i);
System.out.println(j);
853555573
853555573
equals
image.png
- byte、 short、 int、 long、 float、 double、 boolean 、char
- Object、 T(泛型)
使用效果
Integer[] array1 = {1, 1, 1, 2, 7, 11, 22, 37};
Integer[] array2 = {1, 1, 1, 2, 7, 11, 22, 37};
boolean equals = Arrays.equals(array1, array2);
System.out.println(equals);
boolean equals1 = Arrays.equals(array1, 2, 4, array2, 2, 4);
System.out.println(equals1);
boolean equals3 = Arrays.equals(array1, array2, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return (o1 <o2 ? -1 : (o1.equals(o2) ? 0 : 1));
}
});
System.out.println(equals3);
boolean equals4 = Arrays.equals(array1, array2, (o1, o2) -> (o1 <o2 ? -1 : (o1.equals(o2) ? 0 : 1)));
System.out.println(equals4);
true
true
true
true
deepEquals
deepHashCode
deepToString
image.png
copyOf
image.png
- byte、 short、 int、 long、 float、 double、 boolean 、char
- T(泛型)、U(泛型)
使用效果
Integer[] array1 = {1, 1, 1, 2, 7, 11, 22, 37};
Integer[] integers = Arrays.copyOf(array1, 4);
System.out.println(Arrays.toString(integers));
String[] s1 = {"wyy", "wzz", "wxx", "wxx"};
String[] strArr = Arrays.copyOf(s1, 4);
System.out.println(Arrays.toString(strArr));
[1, 1, 1, 2]
[wyy, wzz, wxx, wxx]
copyOfRange
image.png
- byte、 short、 int、 long、 float、 double、 boolean 、char
- T(泛型)、U(泛型)
使用效果
Integer[] array1 = {1, 1, 1, 2, 7, 11, 22, 37};
Integer[] integers = Arrays.copyOfRange(array1, 1, 4);
System.out.println(Arrays.toString(integers));
String[] s1 = {"wyy", "wzz", "wxx", "wxx"};
String[] strArr = Arrays.copyOfRange(s1, 1, 4);
System.out.println(Arrays.toString(strArr));
[1, 1, 2]
[wzz, wxx, wxx]
asList
Spliterator
image.png
并行相关、先不花精力研究