如何用java中的流代替for循环


class Checker{

void CheckingNameFromDeptCode(LinkedList<Employee1> empObj, String deptID) 
{
for (int i = 0; i < empObj.size(); i++) {
if (empObj.get(i).getDeptID().equals(deptID)) {
System.out.println(empObj.get(i).getEmpName());
}
}
}
}

这是我创建的方法,用于检查输入的部门列表中的员工。但是我被告知在java 8中使用streams/lambda进行迭代,而不是我使用的老式for循环。

下面是主要的方法。

import java.util.*;
public class Manager{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
LinkedList<Employee1> employeeObj = new LinkedList<Employee1>();
Checker empBusObj = new Checker();

employeeObj.add(new Employee1("Souvik", "D1", "Development", "L1", "Kolkata"));
employeeObj.add(new Employee1("Anirban", "D2", "HR", "L2", "Bangalore"));
employeeObj.add(new Employee1("Joydeep", "D3", "Design", "L3", "Delhi"));
employeeObj.add(new Employee1("Rakesh", "D2", "HR", "L4", "Pune"));
System.out.print("Enter the choices : ");
int ch = sc.nextInt();
String deptInput; 
String locInput;
switch (ch) 
{
case 1:
System.out.print("Enter the department code : ");
deptInput = sc.next();
deptInput = deptInput.toUpperCase();
empBusObj.CheckingNameFromDeptCode(employeeObj, deptInput); 
break;

如果你的问题只是"我需要使用streams/lambdas",这里有一个使用streams的例子:

public static void main(String[] args) {
String deptID = "DPT-01";
List<Employee> employees = new ArrayList<>();
//                          UUID      DEPTID    NAME
employees.add(new Employee("UID-01", "DPT-01", "Foo"));
employees.add(new Employee("UID-02", "DPT-02", "Bar"));
employees.add(new Employee("UID-03", "DPT-01", "AnEmployee"));
employees.add(new Employee("UID-04", "DPT-03", "AnotherEmployee"));
List<Employee> filtered = employees.stream().filter(emp -> emp.getDeptID().equals(deptID)).collect(Collectors.toList());
filtered.forEach(System.out::println); // Here you can also use the double colon operator!
}

你可以在这里找到更多关于流的信息:https://www.baeldung.com/java-streams

关于这里的双冒号操作符:https://www.baeldung.com/java-8-double-colon-operator

<标题>

编辑有一些评论建议使用StreamforEach()方法,所以这里有一个不收集元素的示例:

public static void main(String[] args) {
String deptID = "DPT-01";
List<Employee> employees = new ArrayList<>();
//                          UUID      DEPTID    NAME
employees.add(new Employee("UID-01", "DPT-01", "Foo"));
employees.add(new Employee("UID-02", "DPT-02", "Bar"));
employees.add(new Employee("UID-03", "DPT-01", "AnEmployee"));
employees.add(new Employee("UID-04", "DPT-03", "AnotherEmployee"));
employees
.stream()
.filter(emp -> emp.getDeptID().equals(deptID))
.forEach(emp -> System.out.println(emp.getEmpName());
}

你有它:Nodouble colon operator, NoCollectors, NotoString()。请记住这只是一个简单的例子。

Btw,这是我使用的Employee类:

public class Employee {
private String userID;
private String deptID;
private String empName;
public Employee(String userID, String deptID, String empName) {
this.userID = userID;
this.deptID = deptID;
this.empName= empName;
}
public String getUserID() {
return userID;
}
public void setUserID(String userID) {
this.userID = userID;
}
public String getDeptID() {
return deptID;
}
public void setDeptID(String deptID) {
this.deptID = deptID;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName= empName;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Employee employee = (Employee) o;
return Objects.equals(empName, employee.empName) && Objects.equals(deptID, employee.deptID) && Objects.equals(userName, employee.userName);
}
@Override
public int hashCode() {
return Objects.hash(empName, deptID, userName);
}
@Override
public String toString() {
return "Employee{" +
"empName='" + empName+ ''' +
", deptID='" + deptID + ''' +
", userName='" + userName + ''' +
'}';
}
}

使用流代码将看起来像这样

empObj.stream().filter(obj -> {
return obj.getDeptID().equals(deptID);
}).forEach(obj -> {
System.out.println(obj.getEmpName());
});

或parallelStream到并发处理:

empObj.parallelStream().filter(obj -> {
return obj.getDeptID().equals(deptID);
}).forEach(obj -> {
System.out.println(obj.getEmpName());
});

你可以用Stream api这样做:

void CheckingNameFromDeptCode(LinkedList<Employee1> empObj, String deptID) 
{
empObj.stream().filter(employer -> 
employer.getId().equals(deptID)).forEach(employer -> 
System.out.println(employer.getEmpName()));
}

相关内容

  • 没有找到相关文章

最新更新