List与数组相互转换

文章目录

将 List 转 数组

List.toArra()

java 复制代码
		List<Integer> list = Lists.newArrayList(1, 2, 3);
		
		// 无需转换类型 --> [1,2,3]
		Integer[] intArray = list.toArray(new Integer[0]);
		

Stream().toArray()

java 复制代码
		List<Integer> list = Lists.newArrayList(1, 2, 3);

		// 需要转换类型 --> ["1","2","3"]
		String[] strArray = list.stream().map(String::valueOf).toArray(String[]::new);
		

将 数组 转 List

Lists.newArrayList()

java 复制代码
		String[] arr = {"1", "2", "3"};
		
		// 无需转换类型 --> ["1","2","3"]
		List<String> list = Lists.newArrayList(arr);
		

Stream.of()

java 复制代码
		String[] arr = {"1", "2", "3"};
		
		// 需要转换类型 --> [1,2,3]
		List<Integer> list = Stream.of(arr).map(Integer::parseInt).collect(Collectors.toList());
		
相关推荐
阿豪学编程2 小时前
面试题map/unordered相关
数据结构
武藤一雄2 小时前
19个核心算法(C#版)
数据结构·windows·算法·c#·排序算法·.net·.netcore
梦想的颜色2 小时前
mongoTemplate + Java 增删改查基础介绍
数据结构·数据库·mysql
叶小鸡4 小时前
小鸡玩算法-力扣HOT100-堆
数据结构·算法·leetcode
LUVK_6 小时前
第七章查找
数据结构·c++·考研·算法·408
khalil10206 小时前
代码随想录算法训练营Day-31贪心算法 | 56. 合并区间、738. 单调递增的数字、968. 监控二叉树
数据结构·c++·算法·leetcode·贪心算法·二叉树·递归
数智化精益手记局7 小时前
人员排班管理软件的自动化功能解析:解决传统手工人员进行排班管理耗时长的难题
运维·数据结构·人工智能·信息可视化·自动化·制造·精益工程
LeocenaY7 小时前
C语言面试题总结
c语言·开发语言·数据结构
睡觉就不困鸭9 小时前
第11天 删除有序数组中的重复项 II
数据结构
im_AMBER9 小时前
Leetcode 160 最小覆盖子串 | 串联所有单词的子串
开发语言·javascript·数据结构·算法·leetcode