如何将具有属性的列表转换为另一个列表的java 8方式?

Iva*_*van 10 java collections refactoring list java-8

列表A包含属性Developer.开发人员架构喜欢:

@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;
    }
}
Run Code Online (Sandbox Code Playgroud)

列出A的属性:

List<Developer> A = ImmutableList.of(new Developer("Ivan", 25), new Developer("Curry", 28));
Run Code Online (Sandbox Code Playgroud)

要求将列表A转换为列表B,列表B具有属性ProductManager,属性与列表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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在过去,我们会编写如下代码:

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;
}
Run Code Online (Sandbox Code Playgroud)

我们如何用Java 8以更优雅和简洁的方式编写上述内容?

Ram*_*A.G 33

你将不得不使用如下的东西:

List<ProductManager> B = A.stream()
        .map(developer -> new ProductManager(developer.getName(), developer.getAge()))
        .collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)

//对于大量的属性,假设属性具有相似的名称//其他明智的用不同的名称引用这个

List<ProductManager> B = A.stream().map(developer -> {
            ProductManager productManager = new ProductManager();
            try {
                PropertyUtils.copyProperties(productManager, developer);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
            return productManager;
        }).collect(Collectors.toList());

        B.forEach(System.out::println);
Run Code Online (Sandbox Code Playgroud)


Sla*_*ava 9

大概是这样的:

List<ProductManager> B = A.stream()
        .map(developer -> new ProductManager(developer.name, developer.age))
        .collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)


Mah*_*esh 5

如果属性较多,可能超过20个,并且构造函数不能直接使用,如何转换?

如果要设置的属性超过 3-4 个,则应使用生成器,如下所示:

List<Developer> A = ImmutableList.of(new Developer("Ivan", 25),
                                     new Developer("Curry", 28));

Function<Developer, ProductManager> converter = dev -> new ProductManagerBuilder()
        .setAge(dev.getAge())
        .setName(dev.getName())
        .setProperty1(dev.getProperty1())
        .setProperty2(dev.getProperty2())
        ...
        .build();

List<ProductManager> productManagers = A.stream()
                                        .map(converter)
                                        .collect(toList());
Run Code Online (Sandbox Code Playgroud)