List、Set、 数组等转字符串

阅读更多
依赖jar包:xwork-core-2.1.6.jar

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.xwork.StringUtils;

/**
 * List、Set、数组转字符串
 * @author RSun
 * @Date 2012-11-01
 */
public class Test {
public static void main(String[] args) {

String str = "";

// list转字符串
List list = new ArrayList();
list.add("A");
list.add("B");
str = StringUtils.join(list.toArray(), ","); // 根据不同切割符返回字符串
//jdk8
//str = String.join(",", list);
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 set = new HashSet();
set.add("AA");
set.add("BB");
set.add("CC");
str = StringUtils.join(set.toArray(), ";");
System.out.println("特殊数组转换:"+str);

}
}

你可能感兴趣的:(List转字符串,Set转字符串,数组等转字符串)