Java List转String,String转List
1.采用java8 字符串拼接
public static void main(String[] args) {
List<String> list = Lists.newArrayList();
list.add("test1");
list.add("test2");
list.add("test3");
String join = String.join(",", list);
System.out.println(join);
}
String str = "a,b,c";
List<String> list1 = Arrays.asList(str.split(","));
2、采用guava的Joiner字符串拼接
public static void main(String[] args) {
List<String> list = Lists.newArrayList();
list.add("test4");
list.add("test5");
list.add("test6");
String join = Joiner.on(",").join(list);
System.out.println(join);
}
3、将list泛型user对象中的某个参数添加到String字符串中
List<User> users = userService.findAll();
List<String> stringJoinList = Lists.newArrayList();
for (User user : users) {
stringJoinList.add(user.getRealname());
}
System.out.println(String.join(",",stringJoinList));
转String
List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");
String str1 = StringUtils.join(list, ",");
String str2 = StringUtils.join(list, "");