Java 8:迭代列表并将项添加到新映射

时间:2017-08-24 19:15:01

标签: java-8

我如何在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);
        }

1 个答案:

答案 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()))

我没有编译它,但你可以这样做。