简体   繁体   English

如何使用 Java 流来创建在嵌套集合的所有元素中迭代的数组?

[英]How can I use the Java stream in order to create an array iterating in all the element of a nested collection?

I am working on a Java application and I am trying to understand if Stream concept can be useful for my use case.我正在开发一个 Java 应用程序,我试图了解Stream概念是否对我的用例有用。

I am trying to do something similar to what I found into an example but my use case seems to be more complex.我正在尝试做一些类似于我在示例中发现的内容,但我的用例似乎更复杂。 This was the original example:这是原始示例:

String[] profili = utente.getRuoli()
                 .stream().map(a -> "ROLE_" + a).toArray(String[]::new);

Basically it is creating an array starting from a stream.基本上它是从一个流开始创建一个数组。

Following my situation.按照我的情况。 I have this main User DTO class representing an user:我有这个代表用户的主要User DTO 类:

@Data
public class User {
    private int id;
    private String firstName;
    private String middleName;
    private String surname;
    private char sex;
    private Date birthdate;
    private String taxCode;
    private String eMail;
    private String pswd;
    private String contactNumber;
    private Date createdAt;
    private Set<Address> addressesList;
    private Set<UserType> userTypes;   
}

The previous DTO class contains this field representing a collection of user types (for example "ADMIN", "READER", "WRITER", etcetc).前面的 DTO 类包含该字段,表示用户类型的集合(例如“ADMIN”、“READER”、“WRITER”等)。 This because an user can have multiple types:这是因为一个用户可以有多种类型:

private Set<UserType> userTypes;

This is the UserType structure:这是用户类型结构:

@Data
public class UserType {
    private int id;
    private String typeName;
    private String description;
    Set<Operation> operations;
}

Also this class contains a field representing a collection of operations (this because a user type can have multiple operation that can perform on the system), this field:该类还包含一个表示操作集合的字段(这是因为一个用户类型可以有多个可以在系统上执行的操作),这个字段:

Set<Operation> operations;

Now I am asking if I can use the stream in a similar way to the original example in order to create an array of all the operations contained into all the user types.现在我问我是否可以以与原始示例类似的方式使用流,以创建包含所有用户类型的所有操作的数组。

Is it possible?是否可以? What could be a smart solution?什么是智能解决方案? Or maybe the best way is the "old style" (iterate on all the object into the userTypes Set, then iterate on all the element into the operations Set and manually add to an array?).或者也许最好的方法是“旧样式”(将所有对象迭代到userTypes Set 中,然后将所有元素迭代到操作Set 中并手动添加到数组中?)。 It will work but what could be a neat and elegant solution using stream?它会起作用,但是使用流的简洁优雅的解决方案是什么? (if exist) (如果存在)

Use flatMap() on the operations Set , with distinct() :在操作Set上使用flatMap() ,并使用distinct()

Operation[] operations = user.getUserTypes().stream()
  .map(UserType::getOperations)
  .flatMap(Set::stream)
  .distinct()
  .toArray(Operation[]::new);

It sounds like what you want is just听起来你想要的只是

userList.stream()
  .flatMap(user -> user.getUserTypes().stream())
  .flatMap(userType -> userType.getOperations().stream())
  .toArray(Operation[]::new);

...assuming you're using getters. ...假设您正在使用吸气剂。 This gives you every operation for every user type for every user.这为每个用户的每个用户类型提供了每个操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM