问题
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