我需要获得员工姓名包含"kumar"且年龄大于26的员工数.我使用Java 8流来迭代收集,我能够找到具有上述条件的员工数.
但是,与此同时,我需要打印员工的详细信息.
这是我使用Java 8流的代码:
public static void main(String[] args) {
List<Employee> empList = new ArrayList<>();
empList.add(new Employee("john kumar", 25));
empList.add(new Employee("raja", 28));
empList.add(new Employee("hari kumar", 30));
long count = empList.stream().filter(e -> e.getName().contains("kumar"))
.filter(e -> e.getAge() > 26).count();
System.out.println(count);
}
Run Code Online (Sandbox Code Playgroud)
传统方式:
public static void main(String[] args){
List<Employee> empList = new ArrayList<>();
empList.add(new Employee("john kumar", 25));
empList.add(new Employee("raja", 28));
empList.add(new Employee("hari kumar", 30));
int count = 0;
for (Employee employee : empList) {
if(employee.getName().contains("kumar")){
if(employee.getAge() > 26)
{
System.out.println("emp details :: …Run Code Online (Sandbox Code Playgroud)