Create an array of string with an element from a list [closed]

怎甘沉沦 提交于 2020-01-03 05:37:42

问题


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

First need to filter the list with distinct IDs.

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());

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.

Something along this:

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

回答1:


It seems what you are looking for is:

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


来源:https://stackoverflow.com/questions/59470049/create-an-array-of-string-with-an-element-from-a-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!