title: How to optimize dataset aggregation on top of java objects dataset
body: <p>could you please support me on stupid question:</p>
  
  <p>I have some Java class: </p>
  
  <pre><code>public class ProbePoint implements Serializable, Cloneable {
      private long arrivalTimeMillis = 0;
      private long captureTimeMillis = 0;
  //...
  }
  public class Trip implements Serializable, Cloneable {
      private ArrayList&lt;ProbePoint&gt; points = new ArrayList&lt;&gt;();
  //...
  }
  </code></pre>
  
  <p>I have <code>Dataset[Trip]</code>. I need to collect some min/max values. What would be better implementation of next:</p>
  
  <pre><code>public class DataRanges implements Serializable {
      private long minCaptureTs;
      private long maxArrivalTs;
  
  }
  
   val timesDs: Dataset[DataRanges] = trips.mapPartitions(t =&gt; {
        var minCaptTime = Long.MaxValue
        var maxArrTime = Long.MinValue
        t.foreach(f =&gt; {
          if (f.points.head &lt; minCaptTime) minCaptTime = f.points.head
          if (f.points.last.getArrivalTimeMillis &gt; maxArrTime) maxArrTime = f.points.last.getArrivalTimeMillis
        })
        Iterator[DataRanges](
          new DataRanges(minStartTime, maxEndTime, minArrTime, maxArrTime))
      })(Encoders.bean(classOf[DataRanges]))
      val times = timesDs.agg(min("minCaptureTs"), max("maxArrivalTs")).head()
  }
  }
  </code></pre>