Java流 - 通过嵌套列表分组(按第二顺序列出) [英] Java stream - groupingBy by a nested list (list in a second order)

查看:492
本文介绍了Java流 - 通过嵌套列表分组(按第二顺序列出)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据结构 -

I have the following data structure -

每个学生的列表,每个学生都持有一个国家列表,每个州都有一个城市列表。

List of Students that each holds a lists of States that each holds a list of cities.

public class Student {
    private int id;
    private String name;
    private List<State> states = new ArrayList<>();
}

public class State {
    private int id;
    private String name;
    private List<City> Cities = new ArrayList<>();
}

public class City {
    private int id;
    private String name;
}

我想得到以下内容。

Map<String, Students> citiesIdsToStudensList;

我写下以下内容

Map<Integer, List<Integer>> statesToStudentsMap = students.stream()
            .flatMap(student -> student.getStates().stream())
            .flatMap(state -> state.getCities().stream())
            .collect(Collectors.groupingBy(City::getId, Collectors.mapping(x -> x.getId(), Collectors.toList())));

但它不能得到我想要的结果。

But it doesn't get me the result I want.

推荐答案

使用Stream API,您需要平面地图两次,并将每个中间学生和城市映射到一个能够抓住学生的元组。

Using the Stream API, you'll need to flat map twice and map each intermediate student and city into a tuple that is capable of holding the student.

Map<Integer, List<Student>> citiesIdsToStudentsList =
    students.stream()
            .flatMap(student -> student.getStates().stream().map(state -> new AbstractMap.SimpleEntry<>(student, state)))
            .flatMap(entry -> entry.getValue().getCities().stream().map(city -> new AbstractMap.SimpleEntry<>(entry.getKey(), city)))
            .collect(Collectors.groupingBy(
                entry -> entry.getValue().getId(),
                Collectors.mapping(Map.Entry::getKey, Collectors.toList())
            ));

然而,使用嵌套的代替循环在这里:

However, it would maybe be cleaner to use nested for loops here:

Map<Integer, List<Student>> citiesIdsToStudentsList = new HashMap<>();
for (Student student : students) {
    for (State state : student.getStates()) {
        for (City city : state.getCities()) {
            citiesIdsToStudentsList.computeIfAbsent(city.getId(), k -> new ArrayList<>()).add(student);
        }
    }
}

此杠杆 computeIfAbsent 填充地图并创建具有相同城市ID的每个学生的列表。

This leverages computeIfAbsent to populate the map and creates a list of each student with the same city id.

这篇关于Java流 - 通过嵌套列表分组(按第二顺序列出)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆