栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

数组与list的转换

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数组与list的转换

数组转list 常用分为int[]类型,String[]类型和Integer[]类型

int[] arr 数组转 list

List listRes = Arrays.stream(arr).boxed().collect(Collectors.toList());

String[] strs 转 list

List listRes = Arrays.asList(strs);

Integer[] 转List

List listRes = Arrays.asList(integerRes);

list 转数组 常用分为 转int[], 转 Integer[], 转 String[]

List 转 Integer[]

Integer[] integerRes = list.toArray(new Integer[list.size()]);

List 转 String[]

String[] strRes= list.toArray(new String[0]);

List 转 int[]

int[] arrRes = list.stream().mapToInt(Integer::valueOf).toArray();


数组间的互转

int[] arr转 Integer[]

Integer[] integerRes = Arrays.stream(arr).boxed().toArray(Integer[]::new);

Integer[] 转 int[]

int[] arrRes = Arrays.stream(integerRes).mapToInt(Integer::valueOf).toArray();

String[] 转 int[]

int[] arrRes = Arrays.asList(strRes).stream().mapToInt(Integer::parseInt).toArray();

或 Arrays.stream(strRes).mapToInt(Integer::parseInt).toArray();


mapToInt(Integer::valueOf)将对象流转为基本类型流

toArray()转为为int数组

Arrays.stream(arr)转为流 将int数组转化为IntStream

boxed()装箱,将基本类型流转换为对象流

toArray(Integer::new)将对象流转换为对象数组

collect(Collectors.toList())将对象流收集为集合,转为List


Arrays.stream(arr) 可以替换成IntStream.of(arr)。

1.使用Arrays.stream将int[]转换成IntStream。

2.使用IntStream中的boxed()装箱。将IntStream转换成Stream。

3.使用Stream的collect(),将Stream转换成List,因此正是List。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/726821.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号