1. String [] to Integer []
1. Use the Arrays.stream() method to convert the String array to a Stream<String> object
2. Use the map() method to convert each String element to an Integer type
3. Use the toArray() method to convert the Stream<Integer> object to an Integer[] array
String[] stringArray = {"1", "2", "3", "4", "5"};
Integer[] arr = Arrays.stream(stringArray)
.map(Integer::valueOf)
.toArray(Integer[]::new);
2. Convert int[] to Integer[]
1. Use the Arrays.stream() method to convert the int array to an IntStream object
2. Use the boxed() method to convert each element in the IntStream object to the corresponding Integer object
3. Use the toArray() method to convert the Stream<Integer> object to an Integer[] array
int[] arr2 = {5, 3, 1, 4, 2};
Integer[] integers = Arrays.stream(arr2).boxed().toArray(Integer[]::new);
3. String[] positive order sorting
String[] stringArray = {"6", "8", "3", "2", "5"};
Arrays.sort(stringArray);
System.out.println(Arrays.toString(stringArray));
//[2, 3, 5, 6, 8]
4. String[] reverse order
1.a is a string or Integer array. Then:
Method 1: Arrays.sort(a, Collections.reverseOrder());Method 2: Arrays.sort(a, Collections.reverseOrder());
String[] stringArray = {"6", "8", "3", "2", "5"};
//方式1
Arrays.sort(stringArray,Comparator.reverseOrder());
//方式2
Arrays.sort(stringArray,Collections.reverseOrder());
System.out.println(Arrays.toString(stringArray));
// [8, 6, 5, 3, 2]
5. Integer[] positive order sorting
Integer[] arr = {5, 3, 1, 4, 2};
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
//[1, 2, 3, 4, 5]
6. Integer[] reverse order
Integer[] arr = {5, 3, 1, 4, 2};
//倒序方式1
Arrays.sort(arr,(a,b)-> b-a);
//倒序方式2
Arrays.sort(arr,Comparator.reverseOrder());
//倒序方式3
Arrays.sort(arr,Collections.reverseOrder());
System.out.println(Arrays.toString(arr));
//[5, 4, 3, 2, 1]