一、StringToList
public static void main(String[] args) {
String string= "小学-初中-高中-大学-硕士-博士";
List<String> list = Arrays.asList(string.split("-")).stream().map(s -> String.format(s.trim())).collect(Collectors.toList());
System.out.println(list);
}
二、ListToString
public static void main(String[] args) {
List<String> list1 = Arrays.asList("小学","初中","高中","大学","硕士","博士");
List<String> list2 = Arrays.asList("小学","初中","高中","大学","硕士","博士");
//方法一:使用流的方式
String string1 = list1.stream().collect(Collectors.joining("-"));
System.out.println(string1);
//如果testList里是TestDemo对象,将testList中所有TestDemo对象的name拼接如下:
//String names = testList.stream().map(TestDemo::getName).collect(Collectors.joining(","));
//方法二:使用String.join()函数
String string2 = String.join("-",list2);
System.out.println(string2);
}