1 for循环实现
List<String> list = new ArrayList<>(Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9"));String result = "";for (String str : list) {result += str + "-";}result = result.substring(0, result.length() - 1);
2stream().reduce()实现
List<String> list = new ArrayList<>(Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9"));String result = list.stream().reduce("", (current, str) -> current + "-" + str);
3Collectors.joining()实现
List<String> list = new ArrayList<>(Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9"));String result = list.stream().collect(Collectors.joining("-"));
4String.join()实现
List<String> list = new ArrayList<>(Arrays.asList("1", "2", "3", "4", "5", "6", "7", "8", "9"));String result = String.join("-", list);
如果觉得《Java 字符串拼接4种实现方法(for循环 stream().reduce() Collectors.joining() String.join())》对你有帮助,请点赞、收藏,并留下你的观点哦!