Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9061883
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:35:44+00:00 2026-06-16T15:35:44+00:00

Suppose I have a class Point and a function to process Point instances class

  • 0

Suppose I have a class Point and a function to process Point instances

class Point { private final int x, y; ... } 
...
void handlePoints(Iterable<Point> points) { for (Point p: points) {...} }

Now I would like to read points from a file. Each line of the file contains two numbers, so I have a function (“factory method”) to create a point from a line.

Point makePoint(String line) { ... }

What should I do now? I can write a function to read the file to a list of points and call the handlePoints function.

List<Point> readPoints(BufferedReader reader) {...} // use makePoint here

void handlePoints(BufferedReader reader) {
   List<Point> points = readPoints(reader); 
   handlePoints(points);
}

Unfortunately this function does not seem particularly elegant since it creates an unnecessary list of points in memory.

Wouldn’t it be better to use iterators ?

void handlePoints(Iterator<Point> points) {...}

Iterator<Point> readPoints(BufferedReader reader) {...} // use makePoint here

void handlePoints(BufferedReader reader) {
   Iterator<Point> points = readPoints(reader); 
   handlePoints(points);
}

Does it make sense? Won’t be this code too “noisy” in Java?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:35:45+00:00Added an answer on June 16, 2026 at 3:35 pm

    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));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have simple class like: class MyClass { private $_prop; public function getProp()
Suppose I have class A { public: void print(){cout<<A; }}; class B: public A
Suppose I have: public class foobar { public int lorem; public int ipsum; }
Suppose I have a class that processes some data: class SomeClass { public: void
Suppose I have two classes: class A { int x; int y; }; class
Suppose I have something like the following: class Point : geometry { ... Point(double
Suppose you have something like the following: class Shape // base class { private:
Suppose I have class Foo(db.Model): bar = db.ReferenceProperty(Bar) foo = Foo.all().get() Is there a
Suppose I have: class Foo { ... }; class Bar : public Foo {
Suppose I have: class Foo { public String Bar { get; set; } }

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.