Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/385.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按批更新列表,然后重新加入_Java_Java 8 - Fatal编程技术网

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