Java8-如何将List转变为逗号分隔的字符串List<String> cities = Arrays.asList("Milan",
"London",
"New York",
"San Francisco");
String citiesCommaSeparated = String.join(",", cities);
System.out.println(citiesCommaSeparated);
//Output: Milan,London,New York,San Francisco
使⽤流的⽅式:
String citiesCommaSeparated = cities.stream()
.collect(Collectors.joining(","));
System.out.println(citiesCommaSeparated);
字符串截取逗号前面的
使⽤流的⽅式,在连接之前操作字符串
String citiesCommaSeparated = cities.stream()
.map(String::toUpperCase)
.collect(Collectors.joining(","));
//Output: MILAN,LONDON,NEW YORK,SAN FRANCISCO

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。