I need to convert List of string to Array of userdefinedType and for array I need to convert string to long.
I have achieved the same using below approach to achieve it
TeamsNumberIdentifier[] securityPolicyIdArray = securityPolicyIds.stream()
.map(securityPolicy -> new TeamsNumberIdentifier(Long.valueOf(securityPolicy)))
.collect(Collectors.toCollection(ArrayList::new))
.toArray(new TeamsNumberIdentifier[securityPolicyIds.size()]);
Is there any better approach to convert this?
You don't need to create a temporary ArrayList. Just use toArray() on the stream:
TeamsNumberIdentifier[] securityPolicyIdArray = securityPolicyIds.stream()
.map(securityPolicy -> new TeamsNumberIdentifier(Long.valueOf(securityPolicy)))
.toArray(TeamsNumberIdentifier[]::new);
But in general, I would tend to avoid arrays in the first place, and use lists instead.
I would write it like this:
securityPolicyIds.stream()
.map(Long::valueOf)
.map(TeamsNumberIdentifier::new)
.toArray(TeamsNumberIdentifier[]::new);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.