java lamdba long list 转成 字符串按逗号分隔
可以使用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/dMY8 著作权归作者所有。请勿转载和采集!