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

  • Home
  • SEARCH
  • 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 7836563
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:12:34+00:00 2026-06-02T14:12:34+00:00

I have a program written in Java that reads in a file that is

  • 0

I have a program written in Java that reads in a file that is simply a list of strings into a LinkedHashMap. Then it takes a second file which consists of two columns and for each row see if the right-hand term matches one of the terms from the HashMap. The problem is it’s running very slow.

Here’s a code snippet, this is where it compares the second file to the HashMap terms:

String output = "";

infile = new File("2columns.txt");
        try {
            in = new BufferedReader(new FileReader(infile));
        } catch (FileNotFoundException e2) {
            System.out.println("2columns.txt" + " not found");
        }

        try {
            fw = new FileWriter("newfile.txt");

            out = new PrintWriter(fw);

            try {
                String str = in.readLine();

                while (str != null) {
                    StringTokenizer strtok = new StringTokenizer(str);

                    strtok.nextToken();
                    String strDest = strtok.nextToken();

                    System.out.println("Term = " + strDest);

                    //if (uniqList.contains(strDest)) {
                    if (uniqMap.get(strDest) != null) {
                        output += str + "\r\n";
                        System.out.println("Matched! Added: " + str);
                    }

                    str = in.readLine();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            out.print(output);

I got a performance boost from switching from an ArrayList initially to the LinkedHashMap but it’s still taking a long time. What can I do to speed this up?

  • 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-02T14:12:36+00:00Added an answer on June 2, 2026 at 2:12 pm

    Your major bottleneck may be that you are recreating a StringTokenizer for every iteration of the while loop. Moving this outside the loop could help considerably. Minor speed ups can be obtained by moving the String definition outside the while loop.

    The biggest speedup will probably come from using a StreamTokenizer. See below for an example.

    Oh and use a HashMap instead of a LinkedHashMap as @Doug Ayers says in the above comments 🙂

    And @ MДΓΓ БДLL’s suggestion of profiling your code is bang on. checkout this Eclipse Profiling Example

        Reader r = new BufferedReader(new FileReader(infile));
    StreamTokenizer strtok = new StreamTokenizer(r);
    String strDest ="";
    while (strtok.nextToken() != StreamTokenizer.TT_EOF) {
        strDest=strtok.sval; //strtok.toString() might be safer, but slower
        strtok.nextToken();
    
        System.out.println("Term = " + strtok.sval);
    
        //if (uniqList.contains(strDest)) {
        if (uniqMap.get(strtok.sval) != null) {
            output += str + "\r\n";
            System.out.println("Matched! Added: " + strDest +" "+ strtok.sval);
        }
    
        str = in.readLine();
    }
    

    One final thought is (and I’m not confident on this one) that writing to a file may also be faster if you do it in one go at the end. i.e. store all your matches in a buffer of some sort and do the writing in one hit.

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

Sidebar

Related Questions

I have written a program that reads in a File object (really an XML
I have written a java program that is actually works as a gui to
I have written a program in Java to read in a text file of
I have a console program written in Java that should respond to single key
I have written a nice program in Java that connects to a gmail account
I have a Java program that stores a lot of mappings from Strings to
I have a program which i have myself written in java, but I want
I have written java programs for Client and Server. But, to run the program
As a Christmas gift I have written a small program in Java to calculate
I have a program written in VB.Net (Visual Studio 2008) that uses a DLL

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.