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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T16:42:13+00:00 2026-06-07T16:42:13+00:00

I am writing code for text processing, and things go a lot faster if

  • 0

I am writing code for text processing, and things go a lot faster if I convert the strings to integers first. To do this I made a Dictionary class, where every time I see a new string, I give it an index, and keep two maps, one from string to int, and one from int to string, so I can easily look up both ways. Here’s the code:

class Dictionary {
    private Map<String, Integer> map;
    private Map<Integer, String> reverse_map;
    private int nextIndex;

    public Dictionary() {
        map = new HashMap<String, Integer>();
        reverse_map = new HashMap<Integer, String>();
        nextIndex = 1;
    }

    public int getIndex(String string) {
        if (!map.containsKey(string)) {
            map.put(string, nextIndex);
            reverse_map.put(nextIndex, string);
            nextIndex++;
        }
        return map.get(string);
    }

    public String getString(int index) {
        // getIndex is always called first, so we don't need to check anything
        return reverse_map.get(index);
    }
}

This has been working fine for me in my single-threaded code. But now I want to give this multiple threads to speed it up more, and I’m not sure how to do it. I thought of using ConcurrentHashMap, but I’m not sure that putIfAbsent will guarantee that I don’t use an index twice. I didn’t want to use Collections.synchronizedMap, because this dictionary is accessed really frequently across the threads and so I probably wouldn’t be much better off than with a single thread, because it blocks on every read and write. Is there a way to make this work?

  • 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-07T16:42:14+00:00Added an answer on June 7, 2026 at 4:42 pm

    The simplest thing is to just label your two methods (getIndex and getString) synchronized. See what kind of speedup you get. Maybe it will be enough.

    To use ConcurrentHashMap, you might try this:

    private AtomicInteger nextIndex;
    public int getIndex(String string) {
        Integer n = map.get(string);
        if (n == null) {
            int idx = nextIndex.getAndIncrement();
            n = map.putIfAbsent(string, idx);
            if (n != null) return n;
            reverse_map.put(idx, string);
            return idx;
        }
        return n;
    }
    

    This may occasionally skip an index if two threads insert the same string at the same time, but it won’t be often.

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

Sidebar

Related Questions

I have this code in PHP writing views to a text-file and just increasing
I'm writing code to import dozens of moderate sized text files into SQL SERVER
I'm writing code to read in a 7x15 block of text in a file
When writing my scenerios, is it possible to not have to hard code text
A common mistake when writing code that reads text from a stream in Java
Why does everyone tell me writing code like this is a bad practice? if
After writing code to populate textboxes from an object, such as: txtFirstName.Text = customer.FirstName;
While writing code for connecting two webparts,I created a two labels. the text value
I'm writing a text/code editing program for my own use in Java/Swing, and was
When writing code, I often place debug messages in the code. The debug messages

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.