How to filter data from DTO and save to array list

Viewed 49

How to filter data from DTO and save to array list.

From my JPA Reposttiory query

List<MyModel> myList = myRepository.findByPlaceContaining(name);

I am getting this result.

[
{name:”david”, roll : 101, id: 101-david, dept: “cs”, type:”admin”, someExtra : “etc”},
{name:”david”, roll : 102, id: 102-david, dept: “ECE”, type:”admin”, someExtra : “etc”},
]

Now in response I just want to send. (Desired Result) :

Name,roll,dept and id.

[
    {name:”david”, roll : 101, id: 101-david, dept: “cs”},
    {name:”david”, roll : 102, id: 102-david, dept: “ECE”},
]

Here is catch. Dept is coming from different table.

My Model is Loke this :

public MyModel(String name, String roll, UUID id,  Dept dept, String admin, String someExtra, Geometry myModel) {
    this.name = name;
    this.roll= roll;
    this.id = id;
    this.dept = dept;
    this.admin = admin;
    this. someExtra = someExtra;
    this. myModel = myModel
}

And this myDto

myDTO :

public MyDto(String id, String name, String roll, String admin, String someExtra) {
    Id = salesRegionId;
    this.name = name;
    this.roll = roll;
    this.admin = admin;
    this.someExtra = someExtra;
}

Now here I am trying converting to get my Desired result.

private List<MyDto> convertMethod(List<MyModel> myList) {
    List<MyDto> myModel = myList.stream().map(this::convertToMyDto)
            .collect(Collectors.toList());
    List<MyDto> myTempList = new ArrayList<>();
    for(MyDto myDto: myModel) {
        MyDto myTempDTO = new MyDto(myDto.getName(), myDto.getId(),myDto.getRoll(), myDto.getCreatorId(), myDto.getCreatorName());
        myTempList.add(myTempDTO);
    }
    return myTempList;
}

Can you please help me to here if I can do this by using any other Data Structure where I can directly covert and add into array list to return.

0 Answers
Related