List、Set、 数组等转字符串

时间:2022-03-24 04:11:44
public class Test {

public static void main(String[] args) {

String str = "";

// list转字符串
List<String> list = new ArrayList<String>();
list.add("A");
list.add("B");
list.add("C");
str = StringUtils.join(list.toArray(), ","); // 根据不同切割符返回字符串
System.out.println("正常List转换:"+ str);
list.clear();
str = StringUtils.join(list.toArray(), ",");
System.out.println("size为0转换:"+ str); // 字符串数组转为字符串
String[] strArray = {"AA", "BB", "CC", "DD"};
str = StringUtils.join(strArray, "-");
System.out.println("正常数组转换:"+ str); strArray = new String[3];
str = StringUtils.join(strArray, "-");
System.out.println("特殊数组转换:"+str); // Integer引用类型转字符串 Integer[] array = new Integer[3];
array[0] = 1;
array[2] = 2;
str = StringUtils.join(array, "-");
System.out.println("Integer转换:"+str); // set转换为字符串
Set<String> set = new HashSet<String>();
set.add("AA");
set.add("BB");
set.add("CC");
str = StringUtils.join(set.toArray(), ";");
System.out.println("特殊数组转换:"+str); } }