gpt4 book ai didi

Java:Flat List> 到 Hierarchical List>

转载 作者:行者123 更新时间:2023-12-03 23:43:18 27 4
gpt4 key购买 nike

这个问题似乎非常复杂,所以我在这里发布这个问题以寻求任何可能的方法来解决这个问题。
我有 map 列表。我再次想要一个 map 列表,但要确保将 map 转换为某种层次结构。
原始数据:(List >)

[
{
"studentId": 101,
"name": "John",
"subjectId": 2001,
"marks": 85,
"street": "Bakers Street",
"state": "LA"
},
{
"studentId": 101,
"name": "John",
"subjectId": 2002,
"marks": 75,
"street": "Bakers Street",
"state": "LA"
},
{
"studentId": 102,
"name": "Shae",
"subjectId": 3001,
"marks": 96,
"street": "Howards",
"state": "NYC"
}
]
此 map 列表要转换为以下 map 列表:(List >)
[
{
"studentId": 101,
"name": "John",
"academics":
[
{
"subjectId": 2001,
"marks": 85
},
{
"subjectId": 2002,
"marks": 75
}
],
"address":
{
"street": "Bakers Street",
"state": "LA"
}
},
{
"studentId": 102,
"name": "Shae",
"academics":
[
{
"subjectId": 3001,
"marks": 96
}
],
"address":
{
"street": "Howards",
"state": "NYC"
}
}
]
作为一个幼稚的解决方案,我尝试手动处理它们(真的很无聊),因此正在寻找使用流或任何其他可能的方式进行处理的任何有效且干净的方法。
更新
天真的解决方案如下
public List<Map<String, Object>> transformResultSet(List<Map<String, Object>> flatDataList) {
List<Map<String, Object>> hierarchicalDataList = new ArrayList<Map<String, Object>>();
Map<String, List<Map<String, Object>>> studentIdToStudentDataListMap = new LinkedHashMap<>();

for (Map<Integer, Object> flatData : flatDataList) {
if (studentIdToStudentDataListMap.get(flatData.get("student_id")) == null) {
studentIdToStudentDataListMap.put(Integer.valueOf(flatData.get("student_id").toString()), new ArrayList<Map<String, Object>>());
}
studentIdToStudentDataListMap.get(Integer.valueOf(flatData.get("student_id").toString())).add(flatData);
}

for (Map.Entry<Integer, List<Map<String, Object>>> studentFlatDataList : studentIdToStudentDataListMap.entrySet()) {
Map<String, Object> studentHierarchicalDataMap = new LinkedHashMap<String, Object>();
Map<String, Object> studentFlatDataMap = studentFlatDataList.getValue().get(0);
studentHierarchicalDataMap.put("studentId", studentFlatDataMap.get("studentId"));
studentHierarchicalDataMap.put("name", studentFlatDataMap.get("name"));

List<Map<String, Object>> academicsList = new ArrayList<Map<String, Object>>();
for (Map<String, Object> studentDetailAcademic : studentFlatDataList.getValue()) {
Map<String, Object> academic = new LinkedHashMap<String, Object>();
academic.put("subjectId", studentDetailAcademic.get("subjectId"));
academic.put("marks", studentDetailAcademic.get("marks"));

academicsList.add(academic);
}
studentHierarchicalDataMap.put("academics", academicsList);

Map<String, Object> address = new LinkedHashMap<String, Object>();
address.put("street", studentFlatDataMap.get("street"));
address.put("state", studentFlatDataMap.get("state"));
studentHierarchicalDataMap.put("address", address);

hierarchicalDataList.add(studentHierarchicalDataMap);
}
return hierarchicalDataList;
}

最佳答案

从您的 json 示例中,您似乎有 List<Object>不是 List<Map<String, Object>> .
所以,只是给你一个想法创建 2 个对象,比方说 StudentDtoMarkDto .
假设输入对象是StudentStudentDtoList<MarkDto>作为成员(member):

Map<String, List<Student>>  map = list.stream().collect(groupingBy(Student::studentId)); 
Map<String, StudentDto> dtoMap = new HashMap<>();
for(Map.Entry<String, List<Student>> entry : map.entrySet()) {
StudentDto stud = new StudentDto();
//assign other studentDto properties

for(Student std : entry.getValue()) {
MarkDto mark = new MarkDto();
mark.setSubjectId(std.getStudentid());
mark.setMark(entry.getMark()));

stud.add(mark);
}

dtoMap.put(String.valueOf(stud.getId()), stud);
}

return dtoMap.stream().collect(Collectors.toList()); // or return the map itself

关于Java:Flat List<Map<String, Object>> 到 Hierarchical List<Map<String, Object>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64441790/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com