There is a List A with property Developer. Developer schema likes that:
@Getter
@Setter
public class Developer {
private String name;
private int age;
public Developer(String name, int age) {
this.name = name;
this.age = age;
}
public Developer name(String name) {
this.name = name;
return this;
}
public Developer name(int age) {
this.age = age;
return this;
}
}
List A with properties:
List<Developer> A = ImmutableList.of(new Developer("Ivan", 25), new Developer("Curry", 28));
It is demanded to convert List A to List B which with property ProductManager and the properties is same as the ones of List A.
@Getter
@Setter
public class ProductManager {
private String name;
private int age;
public ProductManager(String name, int age) {
this.name = name;
this.age = age;
}
public ProductManager name(String name) {
this.name = name;
return this;
}
public ProductManager name(int age) {
this.age = age;
return this;
}
}
In the old days, we would write codes like:
public List<ProductManager> convert() {
List<ProductManager> pros = new ArrayList<>();
for (Developer dev: A) {
ProductManager manager = new ProductManager();
manager.setName(dev.getName());
manager.setAge(dev.getAge());
pros.add(manager);
}
return pros;
}
How could we write the above in a more elegant and brief manner with Java 8?