使用列表中的元素创建字符串数组 [英] Create an array of string with an element from a list

查看:104
本文介绍了使用列表中的元素创建字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

其中有一个带有四个字符串的list->员工详细信息(ID,姓名,经理ID等-所有都是字符串类型).

There is a list with four string in it --> employee details ( IDs, name, manager IDs, and so on -- all of string type).

首先需要使用不同的ID过滤列表.

First need to filter the list with distinct IDs.

需要从列表中的每个对象获取ID并将其存储为ID数组.这就是我要转给其他职能的所有唯一的员工ID.

From which need to take the ID from each object in the list and store it as an array of IDs. This would just be all the distinct ID of employee that I then pass off to a different function.

List<ViewData> list = getDataList();
list = list.parallelStream()
            .filter(distinctByKey(ViewData::getId))
            .collect(Collectors.toList());

我当时正在考虑创建一个本地字符串数组,并使用forEach从每个对象中收集所需的字符串,并将其作为元素存储在列表中,最后将其传递给函数.我该怎么办?我在列表上尝试了forEach,但无法获取如何遍历每个对象并将值存储到数组中的方法.

I was thinking of creating a local string array and use a forEach to collect the required string from each object and store it as an element in the list and finally pass it onto the function. How do I go about it? I have tried forEach on the list and am not able to get how to iterate through each object and store the value into the array.

与此类似的事情:

String[] allIds;
list.forEach(id -> allIds[] = getId);

推荐答案

似乎您正在寻找的是:

String[] allIds = 
    getDataList().parallelStream() 
                 .map(ViewData::getId)
                 .distinct()
                 .toArray(String[]::new);

这篇关于使用列表中的元素创建字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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