title: Iterators to read and process file in Java
body: <p>Suppose I have a class <code>Point</code> and a function to process <code>Point</code> instances</p>
  
  <pre>
  class Point { private final int x, y; ... } 
  ...
  void handlePoints(Iterable&lt;Point&gt; points) { for (Point p: points) {...} }
  </pre>
  
  <p>Now I would like to read <code>points</code> from a file. Each line of the file contains two numbers, so I have a function ("factory method") to create a <code>point</code> from a line.</p>
  
  <pre>
  Point makePoint(String line) { ... }
  </pre>
  
  <p>What should I do now? I can write a function to read the file to a list of <code>points</code> and call the <code>handlePoints</code> function.</p>
  
  <pre>
  List&lt;Point&gt; readPoints(BufferedReader reader) {...} // use makePoint here
  
  void handlePoints(BufferedReader reader) {
     List&lt;Point&gt; points = readPoints(reader); 
     handlePoints(points);
  }
  </pre>
  
  <p>Unfortunately this function does not seem particularly elegant since it creates an unnecessary list of points in memory.</p>
  
  <p>Wouldn't it be better to use <em>iterators</em> ? </p>
  
  <pre>
  void handlePoints(Iterator&lt;Point&gt; points) {...}
  
  Iterator&lt;Point&gt; readPoints(BufferedReader reader) {...} // use makePoint here
  
  void handlePoints(BufferedReader reader) {
     Iterator&lt;Point&gt; points = readPoints(reader); 
     handlePoints(points);
  }
  </pre>
  
  <p>Does it make sense? Won't be this code too "noisy" in Java?</p>