1

Basically I have the following code:

List<Object[]> tes = new ArrayList<>();
        Object[] ar1 = {"John","Cena","2347867686"};
        Object[] ar2 = {"Peter","Smith","978787878"};
        tes.add(ar1);
        tes.add(ar2);

I have the following PersonDto class as follows:

public class PersonDto {

    String name;
    String surname;
    String phone;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

}

That's what I was trying:

for (Object[] object : tes) {

            TestDto testDto = new TestDto();
            for (Object object2 : object) {

                System.out.println(object2);

            }

            System.out.println("second");

        }

But i am not able to know when to set surname, name or phone in the for loop. So basically I am trying to map each array to a PersonDto from the list as follows:

So the expected result would be:

  1. The first person(PersonDto) would have name set as John, surname set as Cena and phone set as 2347867686.
  2. The second person(PersonDto) would have name set as Peter, surname set as Smith and phone set as 978787878.

There can be many arrays in the list and each of them will always have three attributes (name, surname, phone).

2
  • 1
    Do you have tried anything yourself? Commented Jun 8, 2018 at 17:31
  • Either write a constructor that takes in name, surname, and phone as parameters and sets the values in this to the associated values, or create the object and then call their setters (but the first way is preferred) Commented Jun 8, 2018 at 17:35

2 Answers 2

4

It would be much easier if your PersonDto had a constructor:

PersonDto(String name, String surname, String phone) {
    this.name = name;
    this.surname = surname;
    this.phone = phone;
}

Then, just call it:

List<PersonDTO> =
    objets.stream()
          .map(arr -> new PersonDTO((String) arr[0], (String) arr[1], (String) arr[2]))
          .collect(Collector.toList());
Sign up to request clarification or add additional context in comments.

Comments

3
public PersonDto (String name, String surname, String phone){
    this.name = name;
    this.surname = surname;
    this.phone = phone;
}

public PersonDto buildPersonDto(Object[] info){
    return new PersonDto((String)info[0], (String)info[1], (String)info[2]);
}

public List<PersonDto> convertList(ArrayList<Object[]> people){
    ArrayList<PersonDto> result = new ArrayList<>();
    for(Object[] person : people)
        result.add(buildPersonDto(person));
    return result;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.