[英]How can I fix this nested collection code using Java Stream in order to create an array of string from the property of an object?
I am working on a Java application and I am trying to implement the following behavior using Stream concept.我正在研究 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.基本上,它是从 stream 开始创建一个数组。
Following my situation.按照我的情况。 I have this main User DTO class representing an user:我有这个代表用户的主要用户DTO class:
@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 class 包含表示用户类型集合的此字段(例如“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:此外,这个 class 包含一个表示操作集合的字段(这是因为一个用户类型可以有多个可以在系统上执行的操作),这个字段:
Set<Operation> operations;
And this is the Operation class structure:这是操作class 结构:
@Data
public class Operation {
private int id;
private String name;
private String description;
}
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.现在我问我是否可以以与原始示例类似的方式使用 stream 来创建包含所有用户类型的所有操作的数组。
I tried doing in the following way:我尝试按照以下方式进行操作:
String[] operations = user.getUserTypes().stream()
.map(UserType::getOperations)
.flatMap(Set::stream)
.distinct()
.toArray(String[]::new);
But when this code is performed I am obtaining the following exception:但是当执行此代码时,我得到以下异常:
java.lang.ArrayStoreException: arraycopy: element type mismatch: can not cast one of the elements of java.lang.Object[] to the type of the destination array, java.lang.String
at java.base/java.lang.System.arraycopy(Native Method) ~[na:na]
I think that it happens because I have to create an array of String but it is working on Operation instances that is not a String instances.我认为这是因为我必须创建一个 String 数组,但它正在处理不是 String 实例的Operation实例。 In theory I have to put into my new array the value of the name field of the current Operation instance.理论上,我必须将当前Operation实例的name字段的值放入我的新数组中。
How can I try to fix this code?如何尝试修复此代码?
You need to map your Operation
s to the String name
as follows:您需要将 map 您的Operation
改为 String name
,如下所示:
String[] operations = user.getUserTypes().stream()
.map(UserType::getOperations)
.flatMap(Set::stream)
.map(Operation::getName)
.distinct()
.toArray(String[]::new);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.