简体   繁体   中英

Convert List<Map<String, Comparable<>>> to List<Map<String, Object>>

I am using a method that return me a List<Map<String, Comparable<?>>> and I would like to convert the result using a stream() and end up with a List<Map<String, Object>> .

My code is similar to the following:

public List<Map<String, Object>> myMethod() {
    List<Map<String, Comparable<?>>> result = anotherMethod();
    return ?
}

How can I convert the list result using a stream() in order to return a List<Map<String, Object>> ?

Casting of the maps' values to Object should work:

public List<Map<String, Object>> myMethod() {
    return anotherMethod().stream()
        .map(m -> m.entrySet().stream()
                .collect(Collectors.toMap(
                    Map.Entry::getKey, e -> (Object)e.getValue()
                ))
        )
        .collect(Collectors.toList());
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM