要转换为 List ,其实可能很多人都会想到
Arrays.asList
事实上这样也是可以的。
但是这个方法要求你的数组需要使用的是对象。
例如,如果你定义下面 2 个数组的话。
int[] inputArrayA = {3, 7, 1, 2, 8, 4, 5};
Integer[] inputArrayB = {3, 7, 1, 2, 8, 4, 5};
你只能对 inputArrayB 数组进行转换为 List。
Arrays.asList
asList 转换方法。
这个转换就非常简单了,使用下面的代码就可以转换了。
Integer[] inputArrayB = {3, 7, 1, 2, 8, 4, 5};
List<Integer> inListA = Arrays.asList(inputArrayB);
stream
下面的的代码是通过 stream 进行转换的。
在转换的同时还对输入数组进行了排序。
int[] inputArrayA = {3, 7, 1, 2, 8, 4, 5};
List<Integer> inList = Arrays.stream(inputArrayA).sorted().boxed().collect(Collectors.toList());
Comments