byte、short、int、long、double、float数组转list

1.情景展示

java基本数据类型数组如何转list?

2.具体分析

在java当中,我们知道:数组转list的方式是:

List<T> list = Arrays.asList(array);

但是,只知其一不知其二:

通过Arrays.asList方法转换为List,List的元素必须是包装类,不能是基本数据类型。

因为基本数据类型无法不能被泛型化,所以,基本数据类型,无法通过这种方式完成从数组到list的转换。

举个栗子:

输出的是byte[]的内存地址,而不是每个byte元素。

已知:

byte[] bytes = new byte[]{1, 2, 3, 4};
short[] shorts = {5, 6, 7, 8};
int[] ints = new int[4];
ints[0] = 9;
ints[1] = 10;
ints[2] = 11;
ints[3] = 12;
long longs[] = new long[]{13,14,15,16};
double doubles[] = {17,18,19,20};
float[] floats = {21,22,23,24};
char[] chars = {'a','b','c','d'};

需要将这些数组,转list,如何实现?

3.解决方案

方式一:使用封装类;

我们知道,基本数据类型都有属于自己的封装类;

java共有8种基本数据类型,对应的封装类如下:

基本数据类型 对应封装类 封装类包路径
byte Byte java.lang.Byte
short Short java.lang.Short
int Integer java.lang.Integer
long Long java.lang.Long
double Double java.lang.Double
float Float java.lang.Float
char Character java.lang.Character
boolean Boolean java.lang.Boolean
// 基本数据类型
byte[] bytes = new byte[]{1, 2, 3, 4};
Collections.singletonList(bytes).forEach(t -> System.out.println(t));
// 封装类
Byte[] bytes2 = new Byte[]{1, 2, 3, 4};
Arrays.asList(bytes2).forEach(System.out::println);

Byte[] bytes2 = new Byte[]{1, 2, 3, 4};
Short[] shorts2 = {5, 6, 7, 8};
Integer[] integers = {9, 10, 11, 12};
Long longs2[] = new Long[]{13L,14L,15L,16L};
Double doubles2[] = {17D,18D,19D,20D};
Float[] floats2 = {21F,22F,23F,24F};

我们可以看到:当int类型转变成封装类的时候,转成Byte,Short,Integer可以自动完成转换;

而,int转成Long,Double,Float的时候是需要手动转换。

方式二:使用java类;

语法:

List<T> list = Arrays.stream(arrays).boxed().collect(Collectors.toList());

java8的Stream,可以将int, long, double三种基本类型转换成对应的封装类list。

int[] ints = new int[4];
ints[0] = 9;
ints[1] = 10;
ints[2] = 11;
ints[3] = 12;
Arrays.asList(ints).forEach(System.out::print);
System.out.println();
Arrays.stream(ints).boxed().collect(Collectors.toList()).forEach(System.out::println);

long longs[] = new long[]{13,14,15,16};
Arrays.stream(longs).boxed().collect(Collectors.toList()).forEach(System.out::println);
double doubles[] = {17,18,19,20};
Arrays.stream(doubles).boxed().collect(Collectors.toList()).forEach(System.out::print);

除了不能将byte[]转成list之外,int,long,double,基本上已经够用了。 

方式三:使用Google封装的jar包。

所需jar包

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>31.1-jre</version>
</dependency>

对照关系

基本数据类型 对应google类 封装类包路径
byte Bytes com.google.common.primitives.Bytes
short Shorts com.google.common.primitives.Shorts
int Ints com.google.common.primitives.Ints
long Longs com.google.common.primitives.Longs
double Doubles com.google.common.primitives.Doubles
float Floats com.google.common.primitives.Floats
char Chars com.google.common.primitives.Chars
boolean Booleans com.google.common.primitives.Booleans

语法:

封装类.asList(对应基本数据类型数组);

示例:

char[] chars = {'a','b','c','d'};
Chars.asList(chars).forEach(System.out::println);

 

2023年2月10日16:54:50

控制台输出(打印)byte数组

System.out.println(Arrays.toString(bytes));

 

写在最后

  哪位大佬如若发现文章存在纰漏之处或需要补充更多内容,欢迎留言!!!

 相关推荐:

posted @ 2022-04-02 14:51  Marydon  阅读(3455)  评论(0编辑  收藏  举报