Home > front end >  How can I fix this nested collection code using Java Stream in order to create an array of string fr
How can I fix this nested collection code using Java Stream in order to create an array of string fr

Time:11-18

I am working on a Java application and I am trying to implement the following behavior using Stream concept.

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:

@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). 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;

And this is the Operation class structure:

@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.

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. In theory I have to put into my new array the value of the name field of the current Operation instance.

How can I try to fix this code?

CodePudding user response:

You need to map your Operations to the String name as follows:

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