I know this is a simpler question.
But is it possible to convert given list of objects List<Person>
and convert it into Vector<PersonName>
using Java Streams API
public class Person {
String name;
String lastName;
int age;
float salary;
}
public class PersonName {
String name;
String lastName;
}
You should really make a constructor and/or make PersonName
extend Person
. But here is a solution that does not use either:
ArrayList<Person> list = ...;
Vector<PersonName> vect = list.stream().map(t -> {
PersonName name = new PersonName();
name.lastName = t.lastName;
name.name = t.name;
return name;
}).collect(Collectors.toCollection(Vector::new));
Create a two argument constructor for the PersonName
type and then you can do:
Vector<PersonName> resultSet =
peopleList.stream()
.map(p -> new PersonName(p.getName(), p.getLastName()))
.collect(Collectors.toCollection(Vector::new));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With