【各种转换】数组转换成字符串,集合转换成字符串,字符串转集合

版权声明:整理不易,转载请注明出处。 https://blog.csdn.net/linmengmeng_1314/article/details/86499478

1. 基本类型转字符串:

  • 基本数据类型.toString 最快
  • String.valueOf() 次之
  • +" " 最慢
    其中long类型没有.toString方法,包装类型Long有。

2. 将list集合转化成string数组:用toArray

List<String> blist=new ArrayList<String>(); //创建一个list集合
String [] a=new String[blist.size()];   //创建string类型数组
blist.toArray(a);//将list集合转成string数组
 //遍历输出string数组
 Arrays.stream(a).forEach(System.out::println);

3. String数组转集合

        String[]arr = new String[]{"123","345","456"};
        //转为ArrayList
        List<String> list = new ArrayList<>(Arrays.asList(arr));
        list.add("567");
        System.out.println(list);

如果使用下面这种转换需要注意了,这样的是不具备增删的功能的

		String[]arr = new String[]{"123","345","456"};
        List<String> list = Arrays.asList(arr);
        System.out.println(list); 

使用这种方式转来的list的类型是Arrays的一个内部类,拥有的方法数量有限,不具备add 、remove等的常用操作。(虽然这个内部类也叫ArrayList)

list.add("567"); //会报UnsupportedOperationException异常

若要经转化后有增加删除等操作,可转为ArrayList或其他拥有完整操作的list类。

注意:对于int[]数组不能直接这样做,因为asList()方法的参数必须是对象。应该先把int[]转化为Integer[]。对于其他primitive类型的数组也是如此,必须先转换成相应的wrapper类型数组。

 int[] numbers = {7, 7, 8, 9, 10, 8, 8, 9, 6, 5, 4};
 int size = numbers.length;
 Integer[] array = new Integer[size];
 for (int i = 0; i < numbers.length; i++) {
  Integer integer = numbers[i];
  array[i] = integer;
  }
 List list = Arrays.asList(array);

Array、ArrayList和Vector的区别

a. Array是表态连续分配的一片内存区域,与ArrayList相比、不能动态改变大小,通过Arrays进行sort、binarySearch等操作;

b. ArrayList是继承自List的可动态改变大小的数组,和Array一样要求连续分配,内部封闭了一个Object数组,许多方法直接调用Arrays实现;

c. Vector和ArrayList功能基本一致,但Vector是线程安全的。

效率由高到低依次为:Array、ArrayList、Vector。

4. String数组转换成字符串:使用 Apache Commons 组件中的 commons-lang3.jar包

String [] a={"abc","d","ef"};
String str=StringUtils.join(a,",")// 使用逗号隔开

join方法的源码:

扫描二维码关注公众号,回复: 5044739 查看本文章
    /**
     * <p>Joins the elements of the provided array into a single String
     * containing the provided list of elements.</p>
     *
     * <p>No delimiter is added before or after the list.
     * Null objects or empty strings within the array are represented by
     * empty strings.</p>
     *
     * <pre>
     * StringUtils.join(null, *)               = null
     * StringUtils.join([], *)                 = ""
     * StringUtils.join([null], *)             = ""
     * StringUtils.join(["a", "b", "c"], ';')  = "a;b;c"
     * StringUtils.join(["a", "b", "c"], null) = "abc"
     * StringUtils.join([null, "", "a"], ';')  = ";;a"
     * </pre>
     *
     * @param array  the array of values to join together, may be null
     * @param separator  the separator character to use
     * @return the joined String, {@code null} if null array input
     * @since 2.0
     */
    public static String join(final Object[] array, final char separator) {
        if (array == null) {
            return null;
        }
        return join(array, separator, 0, array.length);
    }

可以看到join方法中的第二个参数分隔符,即是数组转字符串时,使用分隔符隔开。

5. String转换成byte[]或者byte[]转换成String

        //Original String
        String string = "hello world";
        
        //Convert to byte[]
        byte[] bytes = string.getBytes();	//或者getBytes("utf-8")
         
        //Convert back to String
        String s = new String(bytes);

猜你喜欢

转载自blog.csdn.net/linmengmeng_1314/article/details/86499478