Home > database >  Dynamically modify the list to add new field in each object of the list
Dynamically modify the list to add new field in each object of the list

Time:12-23

I have Student class as follows:-

public class Student{
   private long id;
   private String name;
   private String school;

}

Now I have list of Students coming from DB -

List<Student> students = apiService.getStudents();

I want to dynamically modify this list such that it will also contain the combination of id and name as a separate field. So it will be like-

public class Student{
   private long id;
   private String name;
   private String school;
   private String idName;
}

I can map it like this -

        students.stream().map(p -> p.getId()   "," p.getName())).
        collect(Collectors.toSet());

but it will not preserve the original list..

So not sure how to do this.

Original List -

id= 576, name = "Kevin", school = "ABC"
id= 577, name = "Varon", school = "DEF"

New List -

id= 576, name = "Kevin", school = "ABC", idName= "576Kevin"
id= 577, name = "Varon", school = "DEF", idName= "577Varon"

CodePudding user response:

If you're using same class

@Setter
@Getter
@ToString
static class Student{
    private long id;
    private String name;
    private String school;
    private String idName; //initially will be null

    public Student(long id, String name, String school) {
        this.id = id;
        this.name = name;
        this.school = school;
    }

}

Use ForEach

list.forEach(student -> student.setIdName(student.id student.name));

Or Streams

list.stream().forEach(student -> student.setIdName(student.id student.name));

If you have two separate classes, one with the idName field and one without as in the question.

List<Student> list = Stream
        .of(new Student(1, "Sam", "School"),
            new Student(2, "Ronald", "School2"))
        .collect(Collectors.toList());

 //Create a List with StudentPOJO object having another field as idName
List<StudentPOJO> newList = list
.stream()
.map(student -> new StudentPOJO(student.id, student.name, student.school, student.id   student.name))

.collect(Collectors.toList());

Classes

Student

@AllArgsConstructor
@Setter
@Getter
@ToString
static class Student{
    private long id;
    private String name;
    private String school;
}

StudentPOJO

@AllArgsConstructor
@Setter
@Getter
@ToString
static class StudentPOJO{
    private long id;
    private String name;
    private String school;
    private String idName;
}

CodePudding user response:

You can create a new list of modified students and add them to the original list like this:

List<Student> modifiedStudents = students.stream().map(s -> {
Student modifiedStudent = new Student();
modifiedStudent.setId(s.getId());
modifiedStudent.setName(s.getName());
modifiedStudent.setSchool(s.getSchool());
modifiedStudent.setIdName(s.getId()   ","   s.getName());
return modifiedStudent;
}).collect(Collectors.toList());
students.addAll(modifiedStudents);

This will preserve the original list and also add the modified students to it.

  •  Tags:  
  • java
  • Related