List 与以逗号分割String类型相互转换

经常使用 List 与以逗号分割String类型相互转换,但是老是会忘记,特意记录下

(1)String转List

String ids= "1,2,3,4,5,6";
List<String> result = Arrays.asList(ids.split(","));

String ids= "1,2,3,4,5,6";
List<String> result = Arrays.asList(ids.split("[,.]"));

String ids= "1,2,3,4,5,6";
String[] result = ids.split(",");
List<String> list = Arrays.asList(result);

(2)List转String

List<String> result = new ArrayList<>();
result.add("a");
result.add("b");
result.add("c");
String ids = String.join(",", result);

List<String> idsList = Arrays.asList("1", "2", "3","4", "5", "6");
String ids = idsList.stream().collect(Collectors.joining(","));

 

posted @ 2020-01-07 12:56  慕尘  阅读(2945)  评论(0编辑  收藏  举报