@Test
void contextLoads() {
List<String> list = new ArrayList<>();
list.add("xxxx");
list.add("xxxx");
list.add("xxxx");
list.add("xxxx");
System.out.println("原来数据=" + list);
String nameStr = join(",", list);
System.out.println("转之后的string 数据=" + nameStr);
List<String> strings = stringToList(nameStr);
System.out.println("还原原来的数据=" + strings);
}
/*
* String to List
*/
private List<String> stringToList(String strs) {
String str[] = strs.split(",");
return Arrays.asList(str);
}
/*
* List to String
*/
public static String join(@NonNull CharSequence delimiter, @NonNull Iterable tokens) {
final Iterator<?> it = tokens.iterator();
if (!it.hasNext()) {
return "";
}
final StringBuilder sb = new StringBuilder();
sb.append(it.next());
while (it.hasNext()) {
sb.append(delimiter);
sb.append(it.next());
}
return sb.toString();
}
网友评论