数组常用操作。以逗号隔开、以逗号+单引号隔开、转List等

String []array = new String[]{"A", "B", "C"};

        {
            System.out.println("数组排序");
            Arrays.sort(array);
            for (String r : array) {
                System.out.println(r);
            }
        }

        {
            System.out.println("数组转List");
            for (String r : Arrays.asList(array)) {
                System.out.println(r);
            }

        }

        {
            System.out.println("List转数组");
            List<String> list = Arrays.asList(array);
            for (String r : list.toArray(new String[list.size()])) {
                System.out.println(r);
            }
        }

        {
            System.out.println("数组转成set集合");
            Set<String> set = new HashSet<>(Arrays.asList(array));
            System.out.println(set);
        }

        {
            System.out.println("数组转字符串,以逗号隔开");
            String s = StringUtils.join(array, ",");
            System.out.println(s);
        }

        {
            System.out.println("数组转字符串,以单引号及逗号分开,一般用于SQL拼IN串");
            String s = StringUtils.join(array, "\',\'");
            String s1 = StringUtils.wrap(s, "\'");
            System.out.println(s1);
        }

输出结果:

数组排序
A
B
C
数组转List
A
B
C
List转数组
A
B
C
数组转成set集合
[A, B, C]
数组转字符串,以逗号隔开
A,B,C
数组转字符串,以单引号及逗号分开,一般用于SQL拼IN串
'A','B','C'

你可能感兴趣的:(java基础)