Best concise method to linearize list of map values?

Pier Luigi :

I've a List of Map like this:

List<Map<String,Object>> l = new ArrayList<>();
Map<String,Integer> m = new HashMap<>();
m.put("X", 1);
m.put("Y", 9);
l.add(m);
m = new HashMap<>();
m.put("X", 23);
m.put("Y", 6);
l.add(m);

What is the best method to linearize all map values with lambda primitives to obtain a list like this

List<Integer> = [1, 9, 23, 6]

?

xingbin :

Try:

List<Integer> result = l
        .stream()
        .flatMap(map -> map.values().stream())
        .collect(Collectors.toList());

System.out.println(result); // [1, 9, 23, 6]

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=91714&siteId=1