英文:
Conversion of loops to Java Streams
问题
以下是您想要翻译的代码部分:
我正在编写这个逻辑,我正在为每个员工分配独立的房间,下面是我要粘贴的代码:
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;
}
现在,我想使用单个Java流语句编写此循环逻辑,但是我尝试了下面的代码,它会将每个房间分配给每个员工,从而创建重复项,而不是将每个房间分配给独立的员工:
while(i < possibleAssigments) {
allEmployeeList.stream()
.map(emp -> createRoomEmployeeList(emp, request.getRooms().get(i)))
.collect(Collectors.toList());
i++;
}
请注意,上述代码中的 HTML 实体(例如 <
和 >
)已被转义为正常的符号。
英文:
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++;
}
答案1
得分: 1
你可以使用 IntStream()
在 i
和 possibleAssigments
之间进行循环,然后返回每次迭代的 RoomEmployeeList
:
x -> createRoomEmployeeList(allEmployeeList.get(x), request.getRooms().get(x)))
最后将它们收集起来。
我没有进行测试,但应该类似于以下内容:
finalList = IntStream.range(i, possibleAssigments).boxed().map(
x -> createRoomEmployeeList(allEmployeeList.get(x), request.getRooms().get(x)))
.collect(Collectors.toList());
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;
}
英文:
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;
}
答案2
得分: 0
我看到的唯一错误是你没有将这个流操作的结果保存在一个变量中。
这可能应该是:
List<EmployeeRooms> finalList = new ArrayList<>();
while(i < possibleAssigments) {
finalList.addAll(allEmployeeList.stream().map(emp -> createRoomEmployeeList(emp, request.getRooms().get(i))).collect(Collectors.toList()));
i++;
}
英文:
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++;
}
答案3
得分: 0
将房间分配给员工
- 迭代0到
possibleAssigments-1
- 获取员工
allEmployeeList.get(i)
和房间request.getRooms().get(i)
- 创建
EmployeeRooms
使用Stream API,可以这样做。
List<EmployeeRooms> finalList =
IntStream.range(0, possibleAssigments)
.boxed()
.map(i -> createRoomEmployeeList(allEmployeeList.get(i),
request.getRooms().get(i)))
.collect(Collectors.toList());
英文:
To assign room to employee
- Iterate 0 to
possibleAssigments-1
- Get employee
allEmployeeList.get(i)
and roomrequest.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());
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论