Java – converts a list of objects with a map into a primitive array

I've been looking for something I need to do, but it's hard for me to put them together First of all, this is my goal. To put it simply:

Object1
    Object2
        Map<String,Double>

What I need to do is to start from the object1 list and get the double [] (all objects in the list have the same N keys in the map) of the map value given a specific key

This is my first attempt:

myList.stream().map(Object1::getObject2).map(Object2::getMyMap).map(m -> m.get(key).collect(Collectors.toCollection(ArrayList::new))

I don't know how to get the original array from here If this is good so far, where do I start? If there is a better way to do it, I am willing to accept the suggestion Thank you for any help, thank you

Solution

use. Maptodouble create a doublestream:

myList.stream()
    .map(Object1::getObject2)
    .map(Object2::getMyMap)
    .mapToDouble(m -> m.get(key))  // or throw if key is not in map
    .toArray();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>