How to convert a list of strings into a list of objects?
You can use groupingBy
to join the different permissions for the same roleName together.
public static final String ROLE_PREFIX = "application.";
public static final String ROLE_READ_PERMISSION = "read";
public static final String ROLE_WRITE_PERMISSION = "write";
@Override
public List<RolePermission> getRoles(Backend backend) {
Map<String, List<String[]>> allRoles = backend.getRoles()
.stream()
.map(s -> s.replace(ROLE_PREFIX, "")) // something like "Role1.read"
.map(s -> s.split("\\.")) // something like ["Role1", "read"]
.collect(Collectors.groupingBy(split -> split[0]));
return allRoles.values()
.stream()
.map(this::buildPermission)
.collect(Collectors.toList());
}
private RolePermission buildPermission(List<String[]> roleEntries) {
RolePermission permission = new RolePermission().setRoleName(roleEntries.get(0)[0]);
roleEntries.stream()
.forEach(entry -> {
if (ROLE_READ_PERMISSION.equals(entry[1]))
permission.setReadAllowed(true);
if (ROLE_WRITE_PERMISSION.equals(entry[1]))
permission.setWriteAllowed(true);
});
return permission;
}
I also think that your String.split
was using an incorrect regex in the original post, because .
is a special regex character. I've tested this and it works correctly.
Output:
[RolePermission(roleName=Role3, readAllowed=true, writeAllowed=false),
RolePermission(roleName=Role2, readAllowed=true, writeAllowed=false),
RolePermission(roleName=Role1, readAllowed=true, writeAllowed=true)]