Java流toArray()转换为特定类型的数组 [英] Java stream toArray() convert to a specific type of array

查看:166
本文介绍了Java流toArray()转换为特定类型的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这很简单,但我实际上是Java 8功能的菜鸟,不知道如何实现这一目标。我有这个包含以下文字的简单行:


Key,Name


我希望将该行转换为String数组,用逗号(,)分隔每个值,但是,我还希望在返回最终数组之前修剪每个字段,所以我执行以下操作:

  Arrays.stream(line.split(,))。map(String :: trim)。指定者(); 

但是,这会返回一个Object []数组而不是String []数组。经过进一步检查,我可以确认内容实际上是String实例,但数组本身是Object元素。让我来说明一下,这是调试器对返回对象的说法:

  Object []:
0 = (字符串)密钥
1 =(字符串)名称

至于我可以说,问题出在地图调用的返回类型中,但是如何让它返回一个String []数组呢?

解决方案

使用 toArray(size - > new String [size]) toArray(String [] :: new)

  String [] strings = Arrays.stream(line.split(,))。map(String ::修剪).toArray(字符串[] ::新); 

这实际上是



<$的lambda表达式p $ p> .toArray(new IntFunction< String []>(){
@Override
public String [] apply(int size){
return new String [size];
}
});

你在告诉将数组转换为相同大小的String数组。



来自 docs


生成器函数取整数,大小为所需的数组,并生成所需大小的数组。这可以用数组构造函数引用简洁地表达:




  Person [] men = people。 stream()
.filter(p - > p.getGender()== MALE)
.toArray(Person [] :: new);

类型参数:


A - 结果数组的元素类型


参数:


generator - 一个产生所需类型和提供长度的新数组的函数



Maybe this is very simple but I'm actually a noob on Java 8 features and don't know how to accomplish this. I have this simple line that contains the following text:

"Key, Name"

and I want to convert that line into a String array, separating each value by the comma (,), however, I also want to trim every field before returning the final array, so I did the following:

Arrays.stream(line.split(",")).map(String::trim).toArray();

However, this returns an Object[] array rather than a String[] array. Upon further inspection, I can confirm that the contents are actually String instances, but the array itself is of Object elements. Let me illustrate this, this is what the debugger says of the returned object:

Object[]:
    0 = (String) "Key"
    1 = (String) "Name"

As far as I can tell, the problem is in the return type of the map call, but how can I make it return a String[] array?

解决方案

Use toArray(size -> new String[size]) or toArray(String[]::new).

String[] strings = Arrays.stream(line.split(",")).map(String::trim).toArray(String[]::new);

This is actually a lambda expression for

.toArray(new IntFunction<String[]>() {
        @Override
        public String[] apply(int size) {
            return new String[size];
        }
    });

Where you are telling convert the array to a String array of same size.

From the docs

The generator function takes an integer, which is the size of the desired array, and produces an array of the desired size. This can be concisely expressed with an array constructor reference:

 Person[] men = people.stream()
                      .filter(p -> p.getGender() == MALE)
                      .toArray(Person[]::new);

Type Parameters:

A - the element type of the resulting array

Parameters:

generator - a function which produces a new array of the desired type and the provided length

这篇关于Java流toArray()转换为特定类型的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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