如何转换strRoles
至Set<Role>
。预先感谢您
Set<String> strRoles = signUpRequest.getRoles();
Set<Role> roles = new HashSet<>();
角色.模型
@Document(collection = "roles")
public class Role {
@Id
private String id;
private ERole name;
}
Erole.enum
public enum ERole {
ROLE_ADMIN,
ROLE_USER
}
最佳答案
使用流:
Set<Role> convertStringSetToRoleSetWithStreams(final Set<String> rolesInString) {
return rolesInString.stream().map(roleInString -> {
final Role role = new Role();
role.setName(ERole.valueOf(roleInString));
return role;
}).collect(Collectors.toSet());
}
没有流:
Set<Role> convertStringSetToRoleSetWithoutStreams(final Set<String> rolesInString) {
final Set<Role> rolesInObject = new HashSet<>();
for (final String roleInString : rolesInString) {
final Role role = new Role();
role.setName(ERole.valueOf(roleInString));
rolesInObject.add(role);
}
return rolesInObject;
}
如果答案有效,请随意接受。
关于java - 如何将 Set<String> 转换为 Set<Object>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61417413/