vikram singh
vikram singh

Reputation: 38

Java 8 : Iterating a list and adding items to new map

How i can write this code in 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);
        }

Upvotes: 0

Views: 52

Answers (1)

Abhijit Sarkar
Abhijit Sarkar

Reputation: 24558

How about the following?

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

I didn't compile it, but you can do that.

Upvotes: 1

Related Questions