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 8992157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:53:23+00:00 2026-06-15T22:53:23+00:00

I need to develop an iterator over a filesystem subtree in Java. The state

  • 0

I need to develop an iterator over a filesystem subtree in Java. The state of the filesystem might change while the iteration is still in progress (e.g. new folders and files get created and deleted). The iterator should therefore first capture a snapshot of the hierarchy (e.g. crawl the tree and save the names of all files found to a list) and then iterate over the snapshot.

I am wondering if it is a good idea or not to put the code to create the cache into the iterator’s constructor. An alternative would be to designate a speciall method for that (named init).

The size and depth of the iterated subtree might get quite large and the caching will therefore be time consuming. Moreover, it might throw IOExceptions (I am still not sure if it is good design practice to throw exceptions from constructors in Java).

On the other hand, creating a dedicated method to initialize the iterator would mean the client code could not use the iterator as simply an implementation of the Iterator interface.

The client code would also be responsible for calling the init method prior to the traversal. I could have the hasNext/next methods first make sure that the iterator has been initialized and if not, call the init method from within them. But that would mean the first call to these methods would be significantly slower than the next ones without any reasons visible from the client side.

  • 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-15T22:53:25+00:00Added an answer on June 15, 2026 at 10:53 pm

    As you said in the comments, I would go for separating the responsibilities in two classes: one for taking the snapshot of the file system (e.g FileSystemSnapshot) and one for iterating it. Depending on the flexibility you need, you can create the FileSystemSnapshot instance in the constructor of the iterator or pass it as a constructor argument. Going in the first direction gives the client more flexibility to configure the iterator and can be valuable if you are planning to have, for example, different strategies for taking file system snapshots. It is also better for unit testing, since it is easy to create mocks or stubs. However, you are forcing the client to know about the traversal details (i.e. that the file system has to be cached before traversing it). Using the second approach hides this implementation details from the client, but is less flexible and a little bit trickier to test (here you could define a createFileSystemSnapshot() method and then mock that method to return a different instance for your tests). You may also want to check the dependency injection pattern.

    HTH

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need develop a Java UI similar to windows explorer. The interface should have
I need to develop an application, which adds telephone numbers to existing or new
I need to develop a new site with the e-commerce part. I'm here to
I need to develop an API for users not familiar with scala (neither Java)
I need to develop the HTTPS application using Flex and Java. But i have
I need to develop an app using Java wrapper for OpenGL LWJGL .The app
I need to develop a new tool, shall we call it box editor: a
I need to develop an application on Android platform that enables a client to
I need to develop a service that will perform login through several login providers
I need to develop a winform with a treeview menu, localizable and with different

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.