Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
谓词在Java中比较两种不同类型列表中的应用_Java_Java Stream_Predicate - Fatal编程技术网

谓词在Java中比较两种不同类型列表中的应用

谓词在Java中比较两种不同类型列表中的应用,java,java-stream,predicate,Java,Java Stream,Predicate,我有两个字符串类型列表和一个对象(考虑Employee)。字符串类型列表包含员工代码。这里我需要检查员工列表是否有任何代码(属性)对象保存在字符串中。下面是我的雇员班 public class Employee { public String code; public String id; // getters, setters and constructor } 在这里,我可以找到员工是否在给定的字符串列表(employeeUserGrpCodes)中保存了代码 publicst

我有两个字符串类型列表和一个对象(考虑Employee)。字符串类型列表包含员工代码。这里我需要检查员工列表是否有任何代码(属性)对象保存在字符串中。下面是我的雇员班

public class Employee {
  public String code;
  public String id;
  // getters, setters and constructor
}
在这里,我可以找到员工是否在给定的字符串列表(employeeUserGrpCodes)中保存了代码

publicstaticvoidmain(字符串[]args){
最终列表employeeUserGrpCodes=Arrays.asList(“ABCWelcome”、“abcplatium”、“SuperEmployee”);
List empList=new ArrayList();
员工k1=新员工(“KCA员工”,“1”);
员工k2=新员工(“ABCWelcome”,“2”);
添加(k1);
添加(k2);
List empListN=empList.stream().filter(i->employeeUserGrpCodes.stream().anyMatch(j->j.equalsIgnoreCase(i.getCode())).collect(Collectors.toList());
ListnewEmpList=empList.stream().map(a->a.getCode()).collect(Collectors.toList()).stream().filter(employeeUserGrpCodes::contains).collect(Collectors.toList());
如果(!empListN.isEmpty()| |!newEmpList.isEmpty())
{
System.out.println(“员工拥有employeeUserGrpCodes”);
}
}
在上面的代码中,列表“empListN”和列表“newEmpList”两种方法都有效。有没有可能在谓词的帮助下做同样的事情?我可以很容易地将谓词放入字符串“anymatch”中,比如

Predicate<Employee> isEmpUserGroup = e -> e.getCode().equalsIgnoreCase(employeeUserGrpCodes.stream())
boolean isRequiredEmployee = empList.stream().anyMatch(isEmpUserGroup);
谓词isEmpUserGroup=e->e.getCode().equalsIgnoreCase(employeeUserGrpCodes.stream())
布尔值isRequiredEmployee=empList.stream().anyMatch(isEmpUserGroup);

首先,为了了解Employee是否有employeeUserGrpCodes,您不需要这两个列表,因为is Employistn不是空的NewEmployist也不是空的,因此我们只能使用这两个列表,然后,与谓词的使用相关,您已经在筛选表达式中使用了它们,您可以在员工列表中使用类似的内容:

Predicate<Employee> employeePredicate = e -> employeeUserGrpCodes.stream().anyMatch(c -> c.equalsIgnoreCase(e.getCode()));
List<Employee> empListN = empList.stream().filter(employeePredicate).collect(Collectors.toList());
1. <您的
newEmpList
中的code>collect(Collectors.toList()).stream()是多余的。2.尽管存在映射,但
equalsIgnoreCase
contains
之间存在细微差别,这在实际处理区分大小写的输入时会很明显。3.
filter
操作中的lambda表达式是一个
谓词
,其中
T
是流的类型,在您的例子中是
Employee
。4.所有这些都可以通过导航到javadoc本身来理解。
Predicate<Employee> employeePredicate = e -> employeeUserGrpCodes.stream().anyMatch(c -> c.equalsIgnoreCase(e.getCode()));
List<Employee> empListN = empList.stream().filter(employeePredicate).collect(Collectors.toList());
if (empList.stream().anyMatch(employeePredicate)) {
    System.out.println("Employee have employeeUserGrpCodes");
}