Iterators to read and process file in Java

随声附和 提交于 2019-12-04 06:52:36

If you don't need to have all points in memory, think of something more along these lines:

while (reader.ready())
{
  String line = reader.readLine();
  Point point = makePoint(line);
  handlePoint(point);
}

How to do this with an iterator and handlePoints: (code for handling exceptions to be added)

class PointIterator implements Iterator<Point>
{
  BufferedReader reader;
  PointIterator(BufferedReader myReader) { reader = myReader; };
  @Override
  public boolean hasNext() { return myReader.ready(); };
  @Override
  public Point next() { return makePoint(myReader.readLine()); };
  @Override
  public void remove()
  { throw new UnsupportedOperationException("Remove not supported!"); };
}

And because handlePoints takes an Iterable:

class PointIterable implements Iterable<Point>
{
  BufferedReader reader;
  public PointIterable(BufferedReader myReader) { reader = myReader; };
  @Override
  public Iterator<Point> iterator() { return new PointIterator(reader); }
}

To use:

handlePoints(new PointIterable(reader));

From a memory point of view, you won't really save any memory by using iterators - I'm guessing you'll be reading all the points into memory, so they'll all have to be stored somehow.

An iterator isn't a different collection type: it is simply a different way of iterating through a collection. For example, you could go list.iterator() to get an iterator to loop through your list (or any collection).

The choice of what collection to use to hold all the points in memory is the one that will affect memory (eg. ArrayList vs. LinkedList).

Just read once from file and have it in memory instead of reading every time from file

List<Points> points ;

public List<Point> readPoints(BufferedReader reader) {
     if(points == null) {
        points = new ArrayList();
        // read from file and populate 
        points.add(point) ;
     }
 return points;
} 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!