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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:11:39+00:00 2026-05-14T04:11:39+00:00

I am trying to improve an external sort implementation in java. I have a

  • 0

I am trying to improve an external sort implementation in java.

I have a bunch of BufferedReader objects open for temporary files. I repeatedly remove the top line from each of these files. This pushes the limits of the Java’s Heap.
I would like a more scalable method of doing this without loosing speed because of a bunch of constructor calls.

One solution is to only open files when they are needed, then read the first line and then delete it. But I am afraid that this will be significantly slower.

So using Java libraries what is the most efficient method of doing this.

–Edit–

For external sort, the usual method is to break a large file up into several chunk files. Sort each of the chunks. And then treat the sorted files like buffers, pop the top item from each file, the smallest of all those is the global minimum. Then continue until for all items.
http://en.wikipedia.org/wiki/External_sorting

My temporary files (buffers) are basically BufferedReader objects. The operations performed on these files are the same as stack/queue operations (peek and pop, no push needed).

I am trying to make these peek and pop operations more efficient. This is because using many BufferedReader objects takes up too much space.

  • 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-05-14T04:11:40+00:00Added an answer on May 14, 2026 at 4:11 am

    I’m away from my compiler at the moment, but I think this will work. Edit: works fine.

    I urge you to profile it and see. I bet the constructor calls are going to be nothing compared to the file I/O and your comparison operations.

    public class FileStack {
      private File file;
      private long position = 0;
      private String cache = null;
    
      public FileStack(File file) {
        this.file = file;
      }
    
      public String peek() throws IOException {
        if (cache != null) {
          return cache;
        }
    
        BufferedReader r = new BufferedReader(new FileReader(file));
        try {
          r.skip(position);
          cache = r.readLine();
          return cache;
        } finally {
          r.close();
        }
      }
    
      public String pop() throws IOException {
        String r = peek();
        if (r != null) {
          // if you have \r\n line endings, you may need +2 instead of +1
          // if lines could end either way, you'll need something more complicated
          position += r.length() + 1;
          cache = null;
        }
        return r;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 374k
  • Answers 374k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Using a component might be the proper way as described… May 14, 2026 at 7:54 pm
  • Editorial Team
    Editorial Team added an answer I think what you're seeing here is a result of… May 14, 2026 at 7:54 pm
  • Editorial Team
    Editorial Team added an answer Aha! We've found a solution! The SQL to do it:… May 14, 2026 at 7:54 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.