简体   繁体   English

如何使用 Java Stream 修复此嵌套集合代码,以便从 object 的属性创建字符串数组?

[英]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);

如何转换 ArrayList<string> 至 ArrayList<object> using.collect(Collectors.toList() 在 Java stream<div id="text_translate"><p> 如何在 Java stream 中使用.collect(Collectors.toList()将ArrayList&lt;String&gt;转换为ArrayList&lt;Object&gt;</p><p> 之前我用过</p><pre>CommonList = detailUtils.SelectIDValueGetterObservable(getActivity()).stream().forEach(i -&gt; CommonList.add(new foo(i));`</pre><p> 但是我遇到了这些<strong>副作用</strong></p><p> <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html</a></p><p> 这建议.collect(Collectors.toList()</p><p> 我们该怎么做</p></div></object></string> - How can I convert ArrayList<String> to ArrayList<Object> using .collect(Collectors.toList() in Java stream

暂无
暂无

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

相关问题 How can I use Java Stream in order to iterate on a collection on object and use a specific string field of each object to create a new array? - How can I use Java Stream in order to iterate on a collection on object and use a specific string field of each object to create a new array? 如何使用 Java 流来创建在嵌套集合的所有元素中迭代的数组? - How can I use the Java stream in order to create an array iterating in all the element of a nested collection? 使用流Java 8获取属性的数组(嵌套属性) - Get Array of Property, of Property (nested property) using stream Java 8 如何从数组创建流? - How can I create a stream from an array? 如何使用 spring 引导从 mongoDB 集合访问嵌套数组? - how can i access nested array from mongoDB collection using spring boot? 如何转换 ArrayList<string> 至 ArrayList<object> using.collect(Collectors.toList() 在 Java stream<div id="text_translate"><p> 如何在 Java stream 中使用.collect(Collectors.toList()将ArrayList&lt;String&gt;转换为ArrayList&lt;Object&gt;</p><p> 之前我用过</p><pre>CommonList = detailUtils.SelectIDValueGetterObservable(getActivity()).stream().forEach(i -&gt; CommonList.add(new foo(i));`</pre><p> 但是我遇到了这些<strong>副作用</strong></p><p> <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html</a></p><p> 这建议.collect(Collectors.toList()</p><p> 我们该怎么做</p></div></object></string> - How can I convert ArrayList<String> to ArrayList<Object> using .collect(Collectors.toList() in Java stream 如何在 Java 中获取数组、集合或字符串的大小? - How can I get the size of an array, a Collection, or a String in Java? 如何将用户从GUI提供的输入存储到Java中的字符串数组? 我正在使用Java swing创建GUI - How can I store inputs given by the user from GUI to a string array in java? I am using java swing to create the GUI 如何筛选清单 <String,Object> 用Java流收集吗? - How to Filter List<String,Object> Collection with java stream? java:从字符串创建嵌套的json对象 - java : create nested json object from String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM