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(","));