Java8-ListChangeString
public class ListChangeString {
public static void main(String[] args) {
List<String> list1 = Arrays.asList("文学","小说","历史","言情","科幻","悬疑");
List<String> list2 = Arrays.asList("文学","小说","历史","言情","科幻","悬疑");
//方案一:使用String.join()函数,给函数传递一个分隔符合一个迭代器,一个StringJoiner对象会帮助我们完成所有的事情
String string1 = String.join("-",list1);
System.out.println(string1);
//方案二:采用流的方式来写
String string2 = list2.stream().collect(Collectors.joining("-"));
System.out.println(string2);
}
}
Java8-StringChangeList
public class StringChangeList {
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);
}
}
原文链接:https://blog.csdn.net/Three_Two/article/details/87699211