Convert Java arrays and list collections to each other

Convert arrays and list collections to each other

1. Conversion between int array and list
Example: int[] array = new int[]{17258, 39322, 17258, 39322, 17258, 52429, 17355, 9830, 17355, 16384}; 1. Convert
int[] to list

// int[]转换为List<Integer>
// Arrays.stream(array)可以替换为IntStream.of(array)
// collect(Collectors.toList())可以直接写为toList();
List<Interger> intList = Arrays.stream(array).boxed().collect(Collectors.toList());
List<Interger> intList = Arrays.stream(array).boxed().toList();
List<Interger> intList = IntStream.of(array).boxed().toList();

2. Convert list to int[]

//List<Integer> 转换为int[]
int[] intArray = intList.stream().mapToInt(Integer::valueOf).toArray();

Note: The conversion of double type is similar to the above.

2. Conversion between String array and list
Example: String[] arrayStr = {“17258”, “39322”, “17258”, “39322”};

//String[]转为List<String>
List<String> strList = Arrays.asList(arrayStr);
//List<String>转为String[]
String strArray = strList.toArray(new String[0])

Reference link

Guess you like

Origin blog.csdn.net/weixin_38863607/article/details/128624405