Stream Convert Objects
// Assume you have User and Person class pojos
public static void main(String[] args) {
List<User> users = Arrays.asList(
new User("Maxi",1),
new User("Priya",2)
);
List<Person> persons = users.stream().map(user -> {
Person person = new Person();
person.setName(user.getName());
person.setId(user.getId());
return person;
}).collect(Collectors.toList());
persons.forEach(System.out::println);
}
Output
Person [name=Maxi, id=1]
Person [name=Priya, id=2]
public static void main(String[] args) {
List<User> users = Arrays.asList(
new User("Maxi",1),
new User("Priya",2)
);
List<Person> persons = users.stream().map(user -> {
Person person = new Person();
person.setName(user.getName());
person.setId(user.getId());
return person;
}).collect(Collectors.toList());
persons.forEach(System.out::println);
}
Output
Person [name=Maxi, id=1]
Person [name=Priya, id=2]