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:
There can be many arrays in the list and each of them will always have three attributes (name, surname, phone).
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());
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments