我如何在Java 8中编写此代码?
for (Iterator<RecordVo> iterator = list.iterator(); iterator.hasNext();) {
RecordVo recordVo = (RecordVo) iterator.next();
ExecutionContext singleThreadExecutionContext = new ExecutionContext();
singleThreadExecutionContext.put("customerId", recordVo.getCustomerId());
singleThreadExecutionContext.put("ThreadName", "Thread-"+recordVo.getCustomerId());
multiThreadExecutionContext.put("Partition - "+recordVo.getCustomerId(), singleThreadExecutionContext);
}
答案 0 :(得分:1)
以下情况如何?
list.stream()
.map(RecordVo::getCustomerId)
.map(id -> {
// create singleThreadExecutionContext as before
return new SimpleImmutableEntry(id, singleThreadExecutionContext);
})
.forEach(e -> multiThreadExecutionContext.put("Partition - " + e.getKey(), e.getValue()))
我没有编译它,但你可以这样做。