简体   繁体   English

将循环转换为 Java 流

[英]Conversion of loops to Java Streams

I am writing this logic where I am assigning individual rooms to each employee, the code of which i am pasting below:我正在编写这个逻辑,我正在为每个员工分配单独的房间,我将其代码粘贴在下面:

public List<EmployeeRooms> assignRoomEmployee(Request request) {
    List<Employee> allEmployeeList = getAllEmployeeList(request);
    int possibleAssigments = Math.min(request.getRooms().size(), allEmployeeList.size());        
    List<EmployeeRooms> finalList = new ArrayList<>();
    int i = 0;
    while(i < possibleAssigments) {                                         
     for (int j= 0; j < allEmployeeList.size(); j++) {                       
         finalList.add(createRoomEmployeeList(allEmployeeList.get(j), request.getRooms().get(i)));                                       
         i++;
         }          
    }
    return finalList;
    
}

Now I would like to write this loop logic using a single Java streams statement but not I have been able to do so correctly, I tried this below code but it assigns each room to each employee thus creating duplicates instead of each room to individual employee:现在我想使用单个 Java 流语句编写此循环逻辑,但我无法正确执行此操作,我尝试了下面的代码,但它将每个房间分配给每个员工,从而创建重复而不是每个房间给单个员工:

 while(i < possibleAssigments) {             
     allEmployeeList.stream()
                    .map(emp -> createRoomEmployeeList(emp, request.getRooms().get(i)))
                    .collect(Collectors.toList());
     i++;
 }

You can use IntStream() to loop between i and possibleAssigments then return a RoomEmployeeList for each iteration:您可以使用IntStream()ipossibleAssigments之间循环,然后为每次迭代返回一个RoomEmployeeList

x -> createRoomEmployeeList(allEmployeeList.get(x), request.getRooms().get(x)))

and finally collect them.最后收集它们。

I didn't tested but it should be something like this:我没有测试,但它应该是这样的:

finalList = IntStream.range(i, possibleAssigments).boxed().map(
                x -> createRoomEmployeeList(allEmployeeList.get(x), request.getRooms().get(x)))
                .collect(Collectors.toList());

The assignRoomEmployee() method: assignRoomEmployee()方法:

public List<EmployeeRooms> assignRoomEmployee(Request request) {
    List<Employee> allEmployeeList = getAllEmployeeList(request);
    int possibleAssigments = Math.min(request.getRooms().size(), allEmployeeList.size());
    int i = 0;
    
    List<EmployeeRooms> finalList = IntStream.range(i, possibleAssigments).boxed().map(
            x -> createRoomEmployeeList(allEmployeeList.get(x), request.getRooms().get(x)))
            .collect(Collectors.toList());

    return finalList;
}

The only mistake I see is that you don't save the result of this stream action in a variable.我看到的唯一错误是您没有将此 stream 操作的结果保存在变量中。

This should probably be这应该是

 List<EmployeeRooms> finalList = new ArrayList<>();
 while(i < possibleAssigments) {             
     finalList.addAll(allEmployeeList.stream().map(emp -> createRoomEmployeeList(emp, request.getRooms().get(i))).collect(Collectors.toList()));
     i++;
 }

To assign room to employee为员工分配房间

  • Iterate 0 to possibleAssigments-1迭代 0 到possibleAssigments-1
  • Get employee allEmployeeList.get(i) and room request.getRooms().get(i)获取员工allEmployeeList.get(i)和房间request.getRooms().get(i)
  • create EmployeeRooms创建EmployeeRooms

Using Stream API you can do this way.使用 Stream API 您可以这样做。

List<EmployeeRooms> finalList = 
    IntStream.range(0, possibleAssigments)
             .boxed()
             .map(i -> createRoomEmployeeList(allEmployeeList.get(i),
                                              request.getRooms().get(i)))
             .collect(Collectors.toList());

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

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