Java Lambda: 将 Long 列表转换为逗号分隔的字符串
可以使用 Java 8 的 Lambda 表达式和 Stream API 来实现将一个长列表转换为逗号分隔的字符串。以下是示例代码:
List<Long> longList = Arrays.asList(1L, 2L, 3L, 4L, 5L);
String commaSeparatedString = longList.stream()
.map(Object::toString)
.collect(Collectors.joining(", "));
System.out.println(commaSeparatedString); // Output: 1, 2, 3, 4, 5
在这个例子中,我们首先将 Long 列表转换为 Stream,然后使用 map 函数将每个 Long 对象转换为字符串。最后,我们使用 Collectors.joining 方法将所有字符串连接成一个逗号分隔的字符串。
原文地址: https://www.cveoy.top/t/topic/nKVw 著作权归作者所有。请勿转载和采集!