List转字符串

List:["a","b","c"]

转换后:a,b,c

1、String.join

java 复制代码
  		// 1. 创建一个List集合 数量不可变
		List<String> list = List.of("a", "b", "c");
		//list = [a, b, c]
		System.out.println("list = " + list);
		String join = String.join(",", list);
		//join = a,b,c
		System.out.println("join = " + join);

2、谷歌工具类 Joiner

import com.google.common.base.Joiner;

java 复制代码
  		// 1. 创建一个List集合 数量不可变
		List<String> list = List.of("a", "b", "c");
		//list = [a, b, c]
		System.out.println("list = " + list);
		String join = Joiner.on(",").join(list);
		//join = a,b,c
		System.out.println("join = " + join);

3、forEach

java 复制代码
  		// 1. 创建一个List集合 数量不可变
		List<String> list = List.of("a", "b", "c");
		//list = [a, b, c]
		System.out.println("list = " + list);
		StringBuffer join = new StringBuffer();
		//和list.forEach没啥区别
		//list.stream().forEach(s -> join.append(s).append(","));
		//可以并行处理
		//list.parallelStream().forEach(s -> join.append(s).append(","));
		list.forEach(s -> join.append(s).append(","));
		//join = a,b,c
		System.out.println("join = " + join);

4、list.stream

java 复制代码
 		// 1. 创建一个List集合 数量不可变
		List<String> list = List.of("a", "b", "c");
		//list = [a, b, c]
		System.out.println("list = " + list);
		//map(String::toString)将流中的每个元素转换为字符串
		String join = list.stream().map(String::toString).collect(Collectors.joining(","));
		//map(String::valueOf)也将流中的每个元素转换为字符串,但对于非字符串类型的元素,会先调用toString()方法转换为字符串
		String join1 = list.stream().map(String::valueOf).collect(Collectors.joining(","));
		//join = a,b,c
		System.out.println("join = " + join);

5、apache 工具类

import org.apache.tomcat.util.buf.StringUtils;

java 复制代码
 		// 1. 创建一个List集合 数量不可变
		List<String> list = List.of("a", "b", "c");
		//list = [a, b, c]
		System.out.println("list = " + list);
		String join = StringUtils.join(list, ',');
		//join = a,b,c
		System.out.println("join = " + join);

import org.apache.commons.lang.StringUtils;

  		// 1. 创建一个List集合 数量不可变
		List<String> list = List.of("a", "b", "c");
		//list = [a, b, c]
		System.out.println("list = " + list);
		//String join = StringUtils.join(list, ",");   单引号,双引号都可以
		String join = StringUtils.join(list, ',');
		//join = a,b,c
		System.out.println("join = " + join);

6、for 循环

java 复制代码
  		StringBuffer join = new StringBuffer();
		// 1. 创建一个List集合 数量不可变
		List<String> list = List.of("a", "b", "c");
		//list = [a, b, c]
		System.out.println("list = " + list);
		for (int i = 0; i < list.size(); i++) {
			join.append(list.get(i));
			if (i != list.size() - 1) {
				join.append(",");
			}
		}
		//join = a,b,c
		System.out.println("join = " + join);
相关推荐
paopaokaka_luck几秒前
基于Spring Boot+Vue的多媒体素材管理系统的设计与实现
java·数据库·vue.js·spring boot·后端·算法
guoruijun_2012_47 分钟前
fastadmin多个表crud连表操作步骤
android·java·开发语言
Hello-Brand17 分钟前
Java核心知识体系10-线程管理
java·高并发·多线程·并发·多线程模型·线程管理
乐悠小码23 分钟前
数据结构------队列(Java语言描述)
java·开发语言·数据结构·链表·队列
史努比.25 分钟前
Pod控制器
java·开发语言
2的n次方_28 分钟前
二维费用背包问题
java·算法·动态规划
皮皮林55128 分钟前
警惕!List.of() vs Arrays.asList():这些隐藏差异可能让你的代码崩溃!
java
莳光.28 分钟前
122、java的LambdaQueryWapper的条件拼接实现数据sql中and (column1 =1 or column1 is null)
java·mybatis
程序猿麦小七33 分钟前
基于springboot的景区网页设计与实现
java·spring boot·后端·旅游·景区
weisian15140 分钟前
认证鉴权框架SpringSecurity-2--重点组件和过滤器链篇
java·安全