将循环转换为Java Streams

huangapple go评论78阅读模式
英文:

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 实体(例如 &lt;&gt;)已被转义为正常的符号。

英文:

I am writing this logic where I am assigning individual rooms to each employee, the code of which i am pasting below :

public List&lt;EmployeeRooms&gt; assignRoomEmployee(Request request) {
	List&lt;Employee&gt; allEmployeeList = getAllEmployeeList(request);
	int possibleAssigments = Math.min(request.getRooms().size(), allEmployeeList.size());   	 
	List&lt;EmployeeRooms&gt; finalList = new ArrayList&lt;&gt;();
	int i = 0;
	while(i &lt; possibleAssigments) {	      		        		   			
	 for (int j= 0; j &lt; 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 &lt; possibleAssigments) {	   		 
	 allEmployeeList.stream()
                    .map(emp -&gt; createRoomEmployeeList(emp, request.getRooms().get(i)))
                    .collect(Collectors.toList());
	 i++;
 }

答案1

得分: 1

你可以使用 IntStream()ipossibleAssigments 之间进行循环,然后返回每次迭代的 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 -&gt; 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 -&gt; createRoomEmployeeList(allEmployeeList.get(x), request.getRooms().get(x)))
				.collect(Collectors.toList());

The assignRoomEmployee() method:

public List&lt;EmployeeRooms&gt; assignRoomEmployee(Request request) {
	List&lt;Employee&gt; allEmployeeList = getAllEmployeeList(request);
	int possibleAssigments = Math.min(request.getRooms().size(), allEmployeeList.size());
	int i = 0;
	
	List&lt;EmployeeRooms&gt; finalList = IntStream.range(i, possibleAssigments).boxed().map(
			x -&gt; 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&lt;EmployeeRooms&gt; finalList = new ArrayList&lt;&gt;();
 while(i &lt; possibleAssigments) {             
     finalList.addAll(allEmployeeList.stream().map(emp -&gt; 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 room request.getRooms().get(i)
  • create EmployeeRooms

Using Stream API you can do this way.

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

huangapple
  • 本文由 发表于 2020年7月21日 20:51:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63014966.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定