英文:
How to filter java stream and return only items starting with a list of provided substrings
问题
Sure, here's the translated code snippet:
我有一个在Spring Boot应用程序中进行用户认证时返回的权限列表。
例如,我获取到的授权列表如下:
user_1_authority,
user_2_authority,
group_1_authority,
group_2_authority,
test_1_authority,
test_2_authority,
driver_x_authority,
...
我想要筛选这些权限,以便只返回以"user_"或"group_"开头的权限。
我想知道如何使用Java流来返回一个新的筛选后的权限列表,使其类似于:
```java
List<String> authoritiesStartingWithList = Arrays.asList("user_", "group_");
public Collection<GrantedAuthorities> filterAuthorities(Authentication authentication, List<String> authoritiesStartingWithList) {
return authentication.getAuthorities().stream().filter(authority -> authoritiesStartingWithList.stream().anyMatch(authority::startsWith)).collect(Collectors.toList());
}
这将返回一个只包含以下内容的列表:
user_1_authority,
user_2_authority,
group_1_authority,
group_2_authority,
<details>
<summary>英文:</summary>
I have a list of authorities returned when user authenticates in Spring Boot app.
For example, I get list of granted authorities like:
user_1_authority,
user_2_authority,
group_1_authority,
group_2_authority,
test_1_authority,
test_2_authority,
driver_x_authority,
...
I would like to filter these authorities so that I get back only authorities starting with "user_" OR "group_".
I wonder how can I use Java streams to return a new list of filtered authorities to have something like:
```java
List<String> authoritiesStartingWithList = Arrays.asList("user_", "group_");
public Collection<GrantedAuthorities> filterAuthorities(Authentication authentication, List<String> authoritiesStartingWithList) {
return authentication.getAuthorities().stream().contains(authoritiesStartingWithList);
}
this would return a list containing only:
user_1_authority,
user_2_authority,
group_1_authority,
group_2_authority,
答案1
得分: 1
尝试以下解决方案:
List<String> authoritiesStartingWithList = Arrays.asList("user_", "group_");
public Collection<GrantedAuthority> filterAuthorities(Authentication authentication, List<String> authoritiesStartingWithList) {
return authentication.getAuthorities().stream().filter(this::startsWithOneOfPredefinedValues).collect(Collectors.toList());
}
private boolean startsWithOneOfPredefinedValues(GrantedAuthority grantedAuthority) {
return authoritiesStartingWithList.stream().anyMatch(i->grantedAuthority.getAuthority().startsWith(i));
}
筛选方法用于迭代前缀列表,以检查是否有匹配项,如果没有找到匹配项,则将权限从流中筛选出。
英文:
Try the following solution:
List<String> authoritiesStartingWithList = Arrays.asList("user_", "group_");
public Collection<GrantedAuthority> filterAuthorities(Authentication authentication, List<String> authoritiesStartingWithList) {
return authentication.getAuthorities().stream().filter(this::startsWithOneOfPredefinedValues).collect(Collectors.toList());
}
private boolean startsWithOneOfPredefinedValues(GrantedAuthority grantedAuthority) {
return authoritiesStartingWithList.stream().anyMatch(i->grantedAuthority.getAuthority().startsWith(i));
}
The filtering method is used to iterate through the list of prefixes to check if there are any matches, if no matches are found, authority is filtered out from the stream.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论