示例
@Test void str2arr() { String ids = "1,2,3,4,5,6"; //字符串 --> String数组 String[] arrayStr = ids.split(","); System.out.println(arrayStr); } @Test void list2Str() { List<String> list = new ArrayList<>(); for (int i = 0; i < 4; i++) { list.add("_" + i); } //List<String> --> ,分隔的String String Str2 = String.join(",", list); } @Test void arr2List() { int[] arr = {1,2,3}; List<int[]> ints = Arrays.asList(arr); System.out.println(ints); Integer[] arr2 = {1,2,3}; List<Integer> ints2 = Arrays.asList(arr2); System.out.println(ints2); // Arrays.asList()返回一个受指定数组支持的固定大小的列表。所以不能做Add、Remove等操作 // ints2.add(4); // System.out.println(ints2); List<Integer> ints3 = new ArrayList<>(ints2); ints3.add(4); System.out.println(ints3); } @Test void arr2List2() { String[] arrayStr = {"1", "2", "3"}; //String数组 --> List<String> List<String> listStr = Arrays.stream(arrayStr).collect(Collectors.toList()); int[] arrayInt = {1, 2, 3}; //int数组 --> List<Integer> List<Integer> listInt = Arrays.stream(arrayInt).boxed().collect(Collectors.toList()); } @Test void arr2Set() { Integer[] arr2 = {1,2,3}; Set<Integer> set = new HashSet<>(Arrays.asList(arr2)); System.out.println(set); } @Test void List2Arr() { List<String> list = new ArrayList<>(); for (int i = 0; i < 4; i++) { list.add("1-"+i); } String[] arr = new String[list.size()]; list.toArray(arr); } @Test void List2Set() { List<String> list = new ArrayList<>(); for (int i = 0; i < 4; i++) { list.add("1-"+i); } Set<String> set = new HashSet<>(list); System.out.println(set); } @Test void Set2List() { Set<String> set = new HashSet<>(); for (int i = 0; i < 4; i++) { set.add("1-"+i); } List<String> list = new ArrayList<>(set); System.out.println(list); } @Test void arr2arr() { String ids = "1,2,3,4,5,6"; //字符串 --> String数组 String[] arrayStr = ids.split(","); //String数组 --> int数组 int[] arrayInt = Arrays.stream(arrayStr).mapToInt(Integer::parseInt).toArray(); // int数组 --> String数组 String[] arrayStr2 = Arrays.stream(arrayInt).mapToObj(String::valueOf).toArray(String[]::new); } @Test void testStream() { List<String> stringList = new ArrayList<>(); stringList.add("zhang3"); stringList.add("lis4"); stringList.add("wang5"); stringList.add("zhao6"); List<Map<String, String>> stringList1 = stringList.stream().map(item -> { Map<String, String> map = new HashMap<>(); map.put("name", item.toUpperCase()); map.put("class", "1"); return map; } ).collect(Collectors.toList()); System.out.println(stringList1); }
参考
https://blog.csdn.net/qq_36095679/article/details/98176723
标签:Java,String,List,list,add,数组,Test,new From: https://www.cnblogs.com/kaituorensheng/p/17766141.html