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());
		
相关推荐
R_AirMan14 分钟前
深入浅出Redis:一文掌握Redis底层数据结构与实现原理
java·数据结构·数据库·redis
科大饭桶1 小时前
数据结构自学Day5--链表知识总结
数据结构·算法·leetcode·链表·c
小高Baby@1 小时前
map数据结构在Golang中是无序的,并且键值对的查找效率较高的原因
数据结构
北风toto1 小时前
python学习DataFrame数据结构
数据结构·python·学习
怀旧,4 小时前
【数据结构】8. 二叉树
c语言·数据结构·算法
凤年徐5 小时前
【数据结构与算法】203.移除链表元素(LeetCode)图文详解
c语言·开发语言·数据结构·算法·leetcode·链表·刷题
浩瀚星辰20247 小时前
C++树状数组详解
java·数据结构·算法
起个数先7 小时前
快速排序算法(Java)
数据结构·排序算法
chao_78915 小时前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode