Java 1.8 list特性用法
list转换成具体的字符串拼接
@Test public void ListToString() { // 构造list List<String> list = Arrays.asList("张三", "李四", "王五", "赵六"); // 以逗号分隔,带前缀后缀 String str1 = list.stream().collect(Collectors.joining(",", "{", "}")); System.out.println("Collectors.joining 带前缀后缀 : " + str1); // 以@分隔,不带前缀后缀 String str2 = list.stream().collect(Collectors.joining("@")); System.out.println("Collectors.joining 不带前缀后缀 : " + str2); } // 输出 Collectors.joining 带前缀后缀 : {张三,李四,王五,赵六} Collectors.joining 不带前缀后缀 : 张三@李四@王五@赵六
带前缀的方式可以拼接成这样的哦
public static void main(String[] args) { List<String> list = Arrays.asList("airTemp","airWet","airLight","airAtmos","windDirectWord","windSpeed","rainFall","airCarbon"); String str1 = list.stream().collect(Collectors.joining("','", "'", "'")); System.out.println(str1); }