如何使用流将java bean列表转换为另一个bean列表

时间:2017-05-21 07:14:00

标签: java java-stream

我有像下面这样的bean

and apply the

在CleanPermission.java中可见,order是在Permission.java中引用AppPermission的权限

我将从服务器(List)获取CleanPermission列表。我想根据权限类型(可见创建单独的Permission对象)将此CleanPermissions列表转换为List。

目前我正在转换如下。但我想用java流转换它。所以,我需要帮助来转换使用java流。

public class CleanPermission {

    private int id;

    private int objectId;

    private boolean visible;
    private boolean order;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getObjectId() {
        return objectId;
    }

    public void setObjectId(int objectId) {
        this.objectId = objectId;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    public boolean isOrder() {
        return order;
    }

    public void setOrder(boolean order) {
        this.order = order;
    }

}

public class Permission {

    private int id;

    private AppPermission appPermission;

    private int objectId;

    private boolean enabled;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public AppPermission getAppPermission() {
        return appPermission;
    }

    public void setAppPermission(AppPermission appPermission) {
        this.appPermission = appPermission;
    }

    public int getObjectId() {
        return objectId;
    }

    public void setObjectId(int objectId) {
        this.objectId = objectId;
    }

    public boolean isEnabled() {
        return enabled;
    }

    public void setEnabled(boolean enabled) {
        this.enabled = enabled;
    }

}

示例数据:

public List<Permission> get(List<CleanPermission> cleanPermissions) {
    List<Permission> permissions = new ArrayList();
    Permission permission;
    for (CleanPermission cleanPermission : cleanPermissions) {
        permission = new Permission();
        permission.setAppObjectType(cleanPermission.getAppObjectType());
        permission.setEnabled(cleanPermission.isVisible());
        permission.setAppPermission(new Permission(){{
            setId(1);
        }});
        permissions.add(permission);
        permission = new Permission();
        permission.setObjectId(cleanPermission.getObjectId());
        permission.setEnabled(cleanPermission.isOrder());
        permission.setAppPermission(new Permission(){{
            setId(2);
        }});
        permissions.add(permission);
    }
    if (!permissions.isEmpty()) {
        System.out.println(permissions.size());
    }
    return permissions;

}

2 个答案:

答案 0 :(得分:1)

有很多方法可以使用流API重构它。首先我建议封装转换方法。

public List<Permission> convert(CleanPermission cp) {
    List<Permission> permissions= new ArrayList<>()
    permission = new Permission();
    permission.setAppObjectType(cleanPermission.getAppObjectType());
    permission.setEnabled(cleanPermission.isVisible());
    permission.setAppPermission(new Permission(){{
        setId(1);
    }});
    permissions.add(permission);
    permission = new Permission();
    permission.setObjectId(cleanPermission.getObjectId());
    permission.setEnabled(cleanPermission.isOrder());
    permission.setAppPermission(new Permission(){{
        setId(2);
    }});
    permissions.add(permission);
    return permissions;
}

和处理

cleanPermissions.stream().map(this::convert)
     .flatMap(Collection::stream).collect(Collectors.toList());

cleanPermissions.stream().map(this::convert)
     .collect(ArrayList::new, ArrayList::addAll, ArrayList::addAll);

答案 1 :(得分:0)

试试这个。

@SafeVarargs
static <T> T build(T t, Consumer<T>... setters) {
    for (Consumer<T> b : setters)
        b.accept(t);
    return t;
}

public List<Permission> get(List<CleanPermission> cleanPermissions) {
    return cleanPermissions.stream()
        .flatMap(c ->  Stream.of(
            build(new Permission(),
                p -> p.setAppObjectType(c.getAppObjectType()),
                p -> p.setEnabled(c.isVisible()),
                p -> p.setAppPermission(
                    build(new Permission(), a -> a.setId(1)))),
            build(new Permission(),
                p -> p.setObjectId(c.getObjectId()),
                p -> p.setEnabled(c.isOrder()),
                p -> p.setAppPermission(
                    build(new Permission(), a ->a.setId(2))))))
        .collect(Collectors.toList());
}