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
本文来自博客园,作者:King-DA,转载请注明原文链接:https://www.cnblogs.com/qingmuchuanqi48/p/12234787.html