package com.google.sps.data;

import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.collections4.keyvalue.MultiKey;

/*
 * Class representing one row/entry in the aggregation response.  A NULL field value
 * means the field was not being aggregated by, and will be omitted from the JSON response
 */
public final class AggregationResponseEntry {

  private String annotatedAssetId;
  private String annotatedLocation;
  private String annotatedUser;
  private final int count;
  private List<String> deviceIds;
  private List<String> serialNumbers;

  public AggregationResponseEntry(
      MultiKey key,
      List<ChromeOSDevice> devices,
      LinkedHashSet<AnnotatedField> fields) {

    String keys[] = (String[]) key.getKeys();
    int currKey = 0;

    Iterator<AnnotatedField> it = fields.iterator();
    while (it.hasNext()) {
      switch(it.next()) {
        case ASSET_ID:
          this.annotatedAssetId = keys[currKey++];
          break;
        case LOCATION:
          this.annotatedLocation = keys[currKey++];
          break;
        case USER:
          this.annotatedUser = keys[currKey++];
          break;
      }
    }

    this.count = devices.size();
    this.deviceIds = devices.stream()
        .map(device -> device.getDeviceId())
        .collect(Collectors.toList());
    this.serialNumbers = devices.stream()
        .map(device -> device.getSerialNumber())
        .collect(Collectors.toList());
  }
}

@@ -23,16 +23,44 @@
 .data;%0A%0A
+import java.util.ArrayList;%0A
 import j
@@ -137,44 +137,8 @@
 st;%0A
-import java.util.stream.Collectors;%0A
 impo
@@ -1248,24 +1248,25 @@
 ces.size();%0A
+%0A
     this.dev
@@ -1278,109 +1278,177 @@
 s = 
-devices.stream
+new ArrayList%3CString%3E();%0A    this.serialNumbers = new ArrayList%3CString%3E
 ()
+;
 %0A    
-    .map(d
+for (ChromeOSD
 evice
- -%3E
  device
-.getDeviceId())%0A        .collect(Collectors.toList
+ : devices) %7B%0A      this.deviceIds.add(device.getDeviceId
 ());%0A
+  
     
@@ -1469,51 +1469,13 @@
 bers
- = devices.stream()%0A        .map(device -%3E 
+.add(
 devi
@@ -1499,47 +1499,15 @@
 r())
+;
 %0A    
-    .collect(Collectors.toList());
+%7D
 %0A  %7D

Improve efficiency by merging two loops accessing the same data into one loop