/**
     * int[],Integer[],List,List<Integer>,HashSet<Integer>相互转换,转成用逗号隔开的字符串
     */
    public static void convertArrayTest1(){
        int[] ints = new int[]{9,4,7,9,2};

        //int[]转Integer[]
        Integer[] integers1 = new Integer[ints.length];
        Arrays.stream(ints).boxed().collect(Collectors.toList()).toArray(integers1);
        //Integer[]转int[]
        int[] ints1 = ArrayUtils.toPrimitive(integers1);

        //Integer[]转List<Integer>
        List<Integer> integerList1 =Arrays.stream(integers1).collect(Collectors.toList());
        //List<Integer>转Integer[]
        Integer[] integers2=new Integer[integerList1.size()];
        integerList1.toArray(integers2);

        //Integer[]转List
        List list1 = Arrays.asList(integers2);
        //List转Integer[]
        Integer[] integers8= (Integer[])list1.toArray();



        //int[]转List<Integer>
        List<Integer> integerList2 =Arrays.stream(ints).boxed().collect(Collectors.toList());
        //List<Integer>转int[]
        int[] ints2 = integerList2.stream().mapToInt(p->p).toArray();




        //Integer[]转HashSet<Integer>
        Set<Integer> set1 = new HashSet<Integer>();
        set1.addAll(Arrays.asList(integers1));
        //HashSet<Integer>转Integer[]
        Integer[] integers3 = new Integer[set1.size()];
        set1.toArray(integers3);


        //int[]转HashSet<Integer>
        Set<Integer> set3 = new HashSet<Integer>();
        set3.addAll(Arrays.stream(ints).boxed().collect(Collectors.toList()));
        //HashSet<Integer>转int[]
        Integer[] integers4 = new Integer[set1.size()];
        set1.toArray(integers4);
        int[] ints4 = ArrayUtils.toPrimitive(integers4);



        //int[]转成用逗号隔开的字符串
        String str1 = ArrayUtils.toString(ints);
        str1 = str1.substring(1,str1.length()-1);

        //Integer[]转成用逗号隔开的字符串
        String str2 = ArrayUtils.toString(integers1);
        str2 = str2.substring(1,str2.length()-1);

        //HashSet<Integer>转成用逗号隔开的字符串
        String str3 = set1.toString();
        str3 = str3.substring(1,str3.length()-1).replace(" ","");

        //List<Integer>转成用逗号隔开的字符串
        String str4 = integerList1.toString();
        str4 = str4.substring(1,str4.length()-1).replace(" ","");


        //int[]转String[] 目前没找到特别牛B的方法
        String[] strings1 = new String[ints.length];
        for(int i=0; i<ints.length;i++){
            strings1[i] = String.valueOf(ints[i]);
        }

        Integer[][] v= new Integer[][]{{4,5},{3,6},{2,7},{1,8},{0,9}};

        //Integer[][]转Map
        Map map = ArrayUtils.toMap(v);
        Object[] ks=map.keySet().toArray();
        Object[] vs =map.values().toArray();

}

    /**
     * String[],List,List<String>,HashSet<String>相互转换,转成用逗号隔开的字符串
     */
    public static void convertArrayTest2() {
        String[] strs = new String[]{"e", "t", "a"};

        //String[]转List
        List list1 = Arrays.asList(strs);
        //List转String[]
        String[] strings1 = (String[]) list1.toArray();

        //String[]转List<String>
        List<String> stringList1 = Arrays.stream(strs).collect(Collectors.toList());
        //List<String>转String[]
        String[] strings2 = new String[stringList1.size()];
        stringList1.toArray(strings2);

        //String[]转HashSet<String>
        Set<String> set1 = new HashSet<String>();
        set1.addAll(Arrays.asList(strs));
        //HashSet<String>转String[]
        String[] strings3 = new String[set1.size()];
        set1.toArray(strings3);


        //List<String>转HashSet<String>
        Set<String> set2 = new HashSet<String>();
        set2.addAll(stringList1);
        //HashSet<String>转List<String>
        String[] strings4= new String[set2.size()];
        set2.toArray(strings4);


        //String[]转成用逗号隔开的字符串
        String str1 = ArrayUtils.toString(strs);
        str1 = str1.substring(1,str1.length()-1);
        //List<String>转成用逗号隔开的字符串
        String str2 = ArrayUtils.toString(stringList1);
        str2 = str2.substring(1,str2.length()-1).replace(" ","");
        //HashSet<String>转成用逗号隔开的字符串
        String str3 = ArrayUtils.toString(set2);
        str3 = str3.substring(1,str3.length()-1).replace(" ","");


    }

 

posted on 2016-10-13 17:08  秦岭过客  阅读(1985)  评论(0编辑  收藏  举报