简体   繁体   English

在 Java 中使用流和过滤器并与可变参数匹配

[英]Using Streams and filter in Java and matching with a varargs

I know that .contains needs a string.我知道.contains需要一个字符串。 Varargs can be a String structure of more than one. Varargs 可以是多个 String 结构。 Like String... role can be "user", "admin" .String... role可以是"user", "admin" Or String... role can be "user" .或者String... role可以是"user"

Whatever is passed into it will be used.传入的任何内容都将被使用。

So I have a filter - where I am trying to see if the getter thats a type String contains the value found in the varargs...所以我有一个过滤器 - 我试图查看作为字符串类型的 getter 是否包含在可变参数中找到的值......

 String a = Arrays.toString(role);
 System.out.print(a);
 TypeOfObject roles = userProjectRoles.stream().filter(userProjectRole ->
    userProjectRole.getRole().getName().equals(a)).findFirst().orElse(null);

a has brackets and it is not a string, but an Array sent as a string value. a 有括号,它不是字符串,而是作为字符串值发送的数组。

Can you help me on how to fix this?你能帮我解决这个问题吗?

Convert input array to Set and use its contains() method将输入数组转换为 Set 并使用其contains()方法

 Set<String> roleSet = new HashSet<>(Arrays.asList(role));
 TypeOfObject roles = userProjectRoles.stream()
     .filter(userProjectRole -> roleSet.contains(userProjectRole.getRole().getName())
     .findFirst()
     .orElse(null);

You can use filter userProjectRoles with the anyMatch method to check if roles contains that role:您可以使用带有 anyMatch 方法的过滤器userProjectRoles来检查roles是否包含该角色:

var result = userProjectRoles.stream()
  .map(role -> role.getRole().getName())
  .filter(roleName -> Arrays.stream(roles).anyMatch(role -> roleName.equals(role)))
  .collect(Collectors.toList())

or

var result = userProjectRoles.stream()
  .map(role -> role.getRole().getName())
  .filter(roleName -> {
    for (String role : roles) if (roleName.equals(role)) return true;
    return false;
  })
  .collect(Collectors.toList())

This will give you all of the elements in userProjectRoles whose names are also in the roles array, if I understand your question correctly.如果我正确理解您的问题,这将为您提供userProjectRoles中名称也在roles数组中的所有元素。

If it's the other way around and you want all the elements in the roles array that are also in userProjectRoles , you can do this:如果相反,并且您希望roles数组中的所有元素也在userProjectRoles中,则可以执行以下操作:

List<String> roleNames = 
  roles.stream()
    .map(role -> role.getRole().getName())
    .collect(Collectors.toList());
var result = Arrays.stream(roles).stream()
  .filter(roleNames::contains)
  .collect(Collectors.toList())

I presume you're trying to get rid of [ and ] from Arrays#toString but it's unclear since there is no direct answer.我认为您正在尝试从 Arrays#toString 中删除 [ 和 ] ,但由于没有直接答案,因此尚不清楚。 If so, this is how.如果是这样,就是这样。

It is worth noting that this is unsafe because the result of role could be multiple values and not a single one.值得注意的是,这是不安全的,因为角色的结果可能是多个值而不是单个值。

String a = Arrays.toString(role).replaceAll("\\[", "").replaceAll("]", "");

An alternative might be to check if the array of roles contains the role name.另一种方法可能是检查角色数组是否包含角色名称。 Ie; IE;

TypeOfObject roles = userProjectRoles.stream()
    .filter(userProjectRole -> Stream.of(a).anyMatch(roleName -> userProjectRole.getRole().getName().equals(roleName))
    .findFirst()
    .orElse(null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM