Java 按批更新列表,然后重新加入
我的目的:更新员工名单的可用性Java 按批更新列表,然后重新加入,java,java-8,Java,Java 8,我的目的:更新员工名单的可用性 List<Employee> employees = ...; //separated to a List of "20 item list" List<List<Employee>> separatedEmployees = ListUtils.partition(employees, 20); //update availability by each "20 item List" separatedEmployees
List<Employee> employees = ...;
//separated to a List of "20 item list"
List<List<Employee>> separatedEmployees = ListUtils.partition(employees, 20);
//update availability by each "20 item List"
separatedEmployees.parallelStream()
.forEach(item -> updateAvailability(user, null, item));
//get back whole list
List<Employee> employeesAfterUpdate= separatedEmployees
.stream()
.flatMap(Collection::stream)
.collect(Collectors.toList());
列出员工=。。。;
//分为“20项清单”
List separatedEmployees=ListUtils.partition(雇员,20);
//按每个“20项清单”更新可用性
parallelStream()
.forEach(项目->更新可用性(用户,空,项目));
//取回全部名单
List employeesAfterUpdate=离职员工
.stream()
.flatMap(集合::流)
.collect(Collectors.toList());
工作完全正常,但我想知道是否有任何不同的实现/方法我不需要创建缓冲区
列出分离的员工
,因为我很难向其他人(几乎没有技术知识)解释列表的列表
您可以使用StreamEx
库来实现这一点:
List<Employee> updatedEmployees = StreamEx.ofSubLists(employees, 20)
.parallel()
.map(items -> updateAvailability(user, null, items))
.flatMap(List::stream)
.collect(Collectors.toList());
List updatedeemployees=streamx.的发布者(员工,20人)
.parallel()
.map(项目->更新可用性(用户、空、项目))
.flatMap(列表::流)
.collect(Collectors.toList());
如果updateAvailability
返回void,则可以将map
替换为peek
,尽管peek
仅用于调试。我建议更改updateAvailability
,这样在阅读后它会返回一个新列表或一个更新的列表,结果我可以使用peek
(而不是forEach
)项,然后在一个流中收集回一个列表
List<Employee> employees = ...;
List<Employee> employeesAfterUpdate = ListUtils.partition(employees, 20)
.parallelStream()
.peek(item -> updateAvailability(user, null, item))
.flatMap(Collection::stream)
.collect(Collectors.toList());
列出员工=。。。;
List employeesAfterUpdate=ListUtils.partition(员工,20名)
.parallelStream()
.peek(项目->更新可用性(用户,空,项目))
.flatMap(集合::流)
.collect(Collectors.toList());
为什么在流式传输列表之前需要拆分列表?你不能在原始列表中调用parallelStream
?@JoakimDanielson,因为我想一次处理20个项目的列表。一次不能有一个项目。所以我把它分成20,然后是流。我们从哪里得到流呢?@DodgyCodeException