如何过滤Java流并仅返回以提供的子字符串列表开头的项目

huangapple go评论54阅读模式
英文:

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 &quot;user_&quot; OR &quot;group_&quot;.

I wonder how can I use Java streams to return a new list of filtered authorities to have something like:

```java
  List&lt;String&gt; authoritiesStartingWithList = Arrays.asList(&quot;user_&quot;, &quot;group_&quot;);

  public Collection&lt;GrantedAuthorities&gt; filterAuthorities(Authentication authentication, List&lt;String&gt; 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&lt;String&gt; authoritiesStartingWithList = Arrays.asList(&quot;user_&quot;, &quot;group_&quot;);

public Collection&lt;GrantedAuthority&gt; filterAuthorities(Authentication authentication, List&lt;String&gt; authoritiesStartingWithList) {
  return authentication.getAuthorities().stream().filter(this::startsWithOneOfPredefinedValues).collect(Collectors.toList());
}

private boolean startsWithOneOfPredefinedValues(GrantedAuthority grantedAuthority) {
  return authoritiesStartingWithList.stream().anyMatch(i-&gt;grantedAuthority.getAuthority().startsWith(i));
}

筛选方法用于迭代前缀列表,以检查是否有匹配项,如果没有找到匹配项,则将权限从流中筛选出。

英文:

Try the following solution:

List&lt;String&gt; authoritiesStartingWithList = Arrays.asList(&quot;user_&quot;, &quot;group_&quot;);

public Collection&lt;GrantedAuthority&gt; filterAuthorities(Authentication authentication, List&lt;String&gt; authoritiesStartingWithList) {
  return authentication.getAuthorities().stream().filter(this::startsWithOneOfPredefinedValues).collect(Collectors.toList());
}

private boolean startsWithOneOfPredefinedValues(GrantedAuthority grantedAuthority) {
  return authoritiesStartingWithList.stream().anyMatch(i-&gt;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.

huangapple
  • 本文由 发表于 2023年6月9日 01:21:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434291.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定