Java 數組與List轉換

示例

 @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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章