Kevin Valdez
Kevin Valdez

Reputation: 389

Remove Element in Nested List with Condition From another List - Java 8

I want to remove items from a list with a conditional value that comes from another List. Here are the objects

public class Student{
    
    private String name;
    private String age;
    private List<Course> listCourses;
    
    //Setters and getters
    
}
    
public Class Course{
    
    private String courseName;
    private List<CourseDetail> listCoursesDetail;
    
    //Setters and getters
}
    
public Class CourseDetail{
    
    private String id;
    private String grade;
    private String professor;
    
    //Setters and getters
    
}

So as you can see the object Student has a list, inside that list there is another list from the object CourseDetail. What I want to achieve is to filter or remove elements from private List<CourseDetail> listCoursesDetail where ID is not equal to id from this other object.

public class Process{
    
    private String name;
    private List<ProcessDetail> listProcessDetail;
    
    //Setters and getters
    
}
    
public class ProcessDetail{

    private String id;
    
    //Setters and getters
}

Assume the object Process is populated as follows

{
  "name": "process1",
  "listProcessDetail": [
    {
      "id": "456"
    },
    {
      "id": "666"
    },
    {
      "id": "555"
    }
  ]
}

Student is populated as follows.

{
  "name": "Kevin",
  "age": "22",
  "listCourses": [
    {
      "courseName": "Math",
      "listCoursesDetail": [
        {
          "id": "666",
          "grade": "88",
          "professor": "Xavier"
        },
        {
          "id": "144",
          "grade": "90",
          "professor": "Marcus"
        },
        {
          "id": "555",
          "grade": "100",
          "professor": "Joe"
        }
      ]
    }
  ]
}

The expected result will be:

{
  "name": "Kevin",
  "age": "22",
  "listCourses": [
    {
      "courseName": "456",
      "listCoursesDetail": [
        {
          "id": "666",
          "grade": "88",
          "professor": "Xavier"
        },
        {
          "id": "555",
          "grade": "100",
          "professor": "Joe"
        }
      ]
    }
  ]
}

The element from listCoursesDetail with ID 144 was removed since there is no such value in the object Process.

So far I have these:

Set<String> ids = Process.getListProcessDetail().stream().map(ProcessDetail::id).collect(Collectors.toSet());

In these I stored all the ID's on a Set.

Then my attempt to remove the items:

List<Course> courses = Student.getListCourses().stream().filter(c -> c.getListCoursesDetail().stream().anyMatch(d -> ids.contains(d.getId()))).collect(Collectors.toList());

With these lines of code I get the same Student object as nothing happened.

Upvotes: 3

Views: 3009

Answers (1)

Andreas
Andreas

Reputation: 159086

Assuming you want to modify the existing objects, not create a clone of them with shorter lists, you'd do it like this:

student.getListCourses().forEach(c ->
    c.getListCoursesDetail().removeIf(d -> ! ids.contains(d.getId())));

Upvotes: 7

Related Questions