Tim
Tim

Reputation: 3

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:

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

Upvotes: 0

Views: 121

Answers (3)

Eklavya
Eklavya

Reputation: 18480

To assign room to employee

  • Iterate 0 to possibleAssigments-1
  • Get employee allEmployeeList.get(i) and room request.getRooms().get(i)
  • create EmployeeRooms

Using Stream API you can do this way.

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

Upvotes: 0

H&#252;lya
H&#252;lya

Reputation: 3433

You can use IntStream() to loop between i and possibleAssigments then return a RoomEmployeeList for each iteration:

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:

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;
}

Upvotes: 1

Ayk Borstelmann
Ayk Borstelmann

Reputation: 316

The only mistake I see is that you don't save the result of this stream action in a variable.

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++;
 }

Upvotes: 0

Related Questions