1.使用String.join()
List<String> strList = new ArrayList<>(); strList.add("aaa"); strList.add("bbb"); String str = String.join(",", strList); System.out.println(str);
结果
aaa,bbb
2.流处理Collectors.joining()
这种方法一般是用于对象的某个字段处理,比如:
List<User> userList = new ArrayList<>(); User user1 = new User(); user1.setId(1); user1.setName("aaa"); userList.add(user1); User user2 = new User(); user2.setId(2); user2.setName("bbb"); userList.add(user2); String str = userList.stream() .map(User::getName) .collect(Collectors.joining(",")); System.out.println(str);
结果:
aaa,bbb
标签:转换成,String,user1,bbb,逗号,add,User,字符串,new From: https://www.cnblogs.com/xkzhangsanx/p/18428089