Convert Array to List in Java
Java Array List primitiveJava version: 8
For object array:
Integer[] nums = {1, 2, 3, 4};
List<Integer> numList = Arrays.asList(nums);
For a primitive Array (need import java.util.stream.Collectors)
int[] nums = {1, 2, 3, 4};
List<Integer> numList = Arrays.stream(nums).boxed().collect(Collectors.toList());
Sort a primitive Array as descending order. It is not in space.
int[] nums = {1, 2, 3, 4};
int[] numsOrdered = Arrays.stream(nums).boxed().sorted(Comparator.reverseOrder()).mapToInt(i->i).toArray();
List
int[] nums = {1, 2, 3, 4};
List<Integer> numList = Arrays.stream(nums).boxed().collect(Collectors.toList());
int[] array = numList.stream().mapToInt(i->i).toArray();
Written on January 30, 2018