简体   繁体   English

比较元素列表并将其添加到另一个列表中

[英]compare and add the list of elements into another list

I am trying to compare and add the list of objects inside another list using java8 stream API.我正在尝试使用 java8 stream API 在另一个列表中比较和添加对象列表。 Below are the sample json.下面是样品 json。

studentList:学生名单:

 [{
  sid:1,
  id:10,
  sname :"xyz"
  sPassOut : "2019"
}, {
  sid:2,
  id:20,
  sname :"abc"
  sPassOut : "2020"
},{
   sid:3,
   id:30,
  sname :"ppp"
  sPassOut : "2021"
},
{
   sid:1,
   id:10,
  sname :"ppp"
  sPassOut : "2021"
},
{
   sid:2,
  id:20,
  sname :"ppp"
  sPassOut : "2021"
}]

myList:我的列表:

  [{
    id:10,
    status:"active",
    studentList:[]
   },{id:20, 
      status:"active",
       studentList:[]
   },{id:30, 
      status:"active",
       studentList:[]
   },{id:40, 
      status:"active",
       studentList:[]
   }]

I want to add the studentList elements in the myList for each matching id value.我想在 myList 中为每个匹配的id值添加 studentList 元素。

tried the below code, but it is not giving the expected result.尝试了下面的代码,但它没有给出预期的结果。

studentList.forEach(studList -> {
    studList.setStudentList(myList.stream().collect(Collectors.toMap(MyList:: getId)));
});

Expected JSON:预期 JSON:

[
    {
    id: 10,
    status: "active",
    studentList: [
            {
  sid: 1,
  id: 10,
  sname : "xyz"
  sPassOut : "2019"
            },
            {
   sid: 4,
   id: 10,
  sname : "ppp"
  sPassOut : "2021"
            }
        ]
    },
    {id: 20, 
      status: "active",
       studentList: [
            {
  sid: 2,
  id: 20,
  sname : "abc"
  sPassOut : "2020"
            },
            {
   sid: 5,
  id: 20,
  sname : "ppp"
  sPassOut : "2021"
            }
        ]
    },
    {id: 30, 
      status: "active",
       studentList: [
            {
   sid: 3,
   id: 30,
  sname : "ppp"
  sPassOut : "2021"
            }
        ]
    },
    {id: 40, 
      status: "active",
       studentList: []
    }
]

I wan to add the elements from studentList grouped together with matching id value in the myList as shown in the sample json above.我想将 studentList 中的元素与 myList 中的匹配 id 值分组在一起,如上面的示例 json 所示。

In order to get the required student list using the stream api, you need to filter the students to get only students with a certain id and then collect them into a list.为了使用 stream api 获得所需的学生列表,您需要过滤学生以仅获取具有特定 ID 的学生,然后将它们收集到列表中。 An example of this is shown below:这方面的一个例子如下所示:

studentList.stream().filter(student -> student.id == ID_TO_MATCH).collect(Collectors.toList())

In the code above, ID_TO_MATCH represents the the student id to find for each section of myList .在上面的代码中, ID_TO_MATCH表示要为myList的每个部分查找的学生 ID。 See the following example of getting ID_TO_MATCH :请参阅以下获取ID_TO_MATCH的示例:

myList.forEach(section -> {
    int ID_TO_MATCH = section.id;
};

These code snippets can be combined to produce the solution:这些代码片段可以组合起来产生解决方案:

myList.forEach(section -> {
    int id = section.id;
    List<Student> studentsFound = studentList.stream().filter(student -> student.id == id).collect(Collectors.toList())
    section.setStudentList(studentsFound);
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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