Collectors.joining
功能作用:拼接字符串
功能示例
public class LambdaDemo {
public static void main(String[] args) {
Artist artist = new Artist();
artist.setMembers(Arrays.asList("1", "2"));
artist.setName("高");
Artist artist1 = new Artist();
artist1.setMembers(Arrays.asList("1", "2"));
artist1.setName("王");
ArrayList<Artist> artists = new ArrayList<>();
artists.add(artist);
artists.add(artist1);
System.out.println(collectArtistName(artists));
System.out.println(collectArtistName1(artists));
System.out.println(collectArtistName3(artists));
}
/**
* 无参
* @param artists
* @return
*/
public static String collectArtistName(List<Artist> artists) {
return artists.stream().map(Artist::getName).collect(Collectors.joining());
}
/**
* 一个参数
* @param artists
* @return
*/
public static String collectArtistName1(List<Artist> artists) {
return artists.stream().map(Artist::getName).collect(Collectors.joining(","));
}
/**
* 三个参数
* @param artists
* @return
*/
public static String collectArtistName3(List<Artist> artists) {
return artists.stream().map(Artist::getName).collect(Collectors.joining(",", "[", "]"));
}
}
结果:
高王
高,王
[高,王]