string数组转为list、list转为数组
注意split数组为空的情况
public static final String SEGMENTATION = ",";
/**
* 将String 转换成 List<Long>
*
* @param str
* @return
*/
public static List<Long> splitStrToListLong(String str) {
List<Long> list = new ArrayList<>();
if (StringUtils.isNotEmpty(str)) {
String[] split = StringUtils.split(str, SEGMENTATION);
for (String string : split) {
list.add(NumberUtils.toLong(string, -1l));
}
}
return list;
}
/**
* 将String 转换成 List<Integer>
*
* @param str
* @return
*/
public static List<Integer> splitStrToListInt(String str) {
List<Integer> list = new ArrayList<>();
if (StringUtils.isNotEmpty(str)) {
String[] split = StringUtils.split(str, SEGMENTATION);
for (String string : split) {
list.add(NumberUtils.toInt(string, -1));
}
}
return list;
}
/**
* 将String 转换成 List<String>
*
* @param str
* @return
*/
public static List<String> splitStrToListString(String str) {
List<String> list = new ArrayList<>();
if (StringUtils.isNotEmpty(str)) {
String[] split = StringUtils.split(str, SEGMENTATION);
//参数中只有一个值
if(split == null){
list.add(str);
return list ;
}
for (String string : split) {
list.add(string);
}
}
return list;
}
/**
* list<String>转string
* @param list
* @param separator
* @return
*/
public static String listToString(List list, char separator) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < list.size(); i++) {
sb.append(list.get(i)).append(separator);
}
return sb.toString().substring(0, sb.toString().length() - 1);
}