Java将字符串数组映射为整数数组 [英] Java map an array of Strings to an array of Integers

查看:529
本文介绍了Java将字符串数组映射为整数数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SO上找到了这段代码,可以将字符串映射为int

I found this code on SO to map strings to ints

Arrays.stream(myarray).mapToInt(Integer::parseInt).toArray();

但是如何使它映射到Integer类型而不是原始int类型?

But how do I make it map to Integer type not the primitive int?

我尝试从Integer.parseInt切换到Integer.valueOf,但是似乎mapToInt()方法强制使用原始类型.

I tried switching from Integer.parseInt to Integer.valueOf, but it seems that the mapToInt() method forces the primitive type.

我有一个整数数组的ArrayList,所以我不能使用原始整数.

I have an ArrayList of arrays of Integers, so I cannot use primitive ints.

推荐答案

由于StringInteger都是引用类型,因此您只需调用Stream::map即可转换数组.

Since String and Integer are both reference types you can simply call Stream::map to transform your array.

Integer[] boxed = Stream.of(myarray).map(Integer::valueOf).toArray(Integer[]::new);

这篇关于Java将字符串数组映射为整数数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆