1. 准备一个逗号分割字符串
String str = "小张,小王,小李,小赵";
2. 逗号分割字符串转换为集合(转换为集合之前会先转换为数组)
List<String> strList1 = Arrays.asList(str.split(","));List<String> strList2 = Arrays.stream(str.split(",")).collect(Collectors.toList());List<Integer> intList = Arrays.stream(str.split(",")).map(Integer::parseInt).collect(Collectors.toList());List<String> strList3= Splitter.on(",").trimResults().splitToList(str);List<String> strList4= Arrays.asList(StringUtils.split(str,","));List<String> strList5 =Arrays.asList(StringUtils.commaDelimitedListToStringArray(str));
3. 集合转换为逗号分隔的字符串
str = String.join(",", strList1);str = StringUtils.join(strList1.toArray(), ",");str = Joiner.on(",").join(strList1);StringJoiner sj = new StringJoiner(",");strList1.forEach(e -> sj.add(String.valueOf(e)));StringJoiner sj2 = new StringJoiner(",", "START_", "_END");strList1.forEach(e -> sj2.add(String.valueOf(e)));str = strList1.stream().collect(Collectors.joining(","));str = strList1.stream().map(e -> {if (e != null) return e.toUpperCase();else return "null";}).collect(Collectors.joining(",", "START_", "_END"));str = StringUtils.collectionToDelimitedString(strList1,",");
4. 数组转逗号分隔字符串
String [] arr = (String[])strList1.toArray();str = StringUtils.join(arr, ",");ArrayUtils.toString(arr, ",");