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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T18:35:55+00:00 2026-05-11T18:35:55+00:00

I’m building a thesaurus using a HashMap <String,ArrayList<String>> to hold words and their synonyms

  • 0

I’m building a thesaurus using a HashMap <String,ArrayList<String>> to hold words and their synonyms (this data structure is required).

For the purpose of the assignment, the synonymity relation is considered transitive. (We can imagine the thesaurus as a graph).
What I’m trying to accomplish is to print this graph in a text file, with a connected component on each line. In other words, all the words that can be pooled together as synonyms should go on a single line.

public void save() {
    try {
        FileWriter fw = new FileWriter(defaultDefinitionFile);
        BufferedWriter out = new BufferedWriter(fw);
        Set<String> keys = thesaurus.keySet();
        Iterator<String> ite = keys.iterator();
        while (ite.hasNext()) {
            String key = ite.next();
            out.write(key);
            ArrayList<String> synonyms = thesaurus.get(key);
            Iterator<String> i = synonyms.iterator();
            while (i.hasNext()) {
                String syn = i.next();
                out.write(","+syn);
                keys.remove(syn);
            }
            out.write("\r\n");
        }
        out.close();
        fw.close();
    }
    catch (Exception e) {
        System.out.println("Error writing to file");
        e.printStackTrace();
    }
}

This is how I pictured it to happen:

Print a word along with each of its synonyms, then remove those synonyms from the data structure so we don’t have duplicate lines.

Problem is of course that I can’t delete anything while i’m iterating over the content of the hashmap.

Any alternative approaches I’m missing?

P.S. I’m keeping the ‘graph’ metaphor throughout only because i needed the title to be eloquent and succint. I understand that this metaphor is limited in usefulness.

  • 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-11T18:35:56+00:00Added an answer on May 11, 2026 at 6:35 pm

    You can store the words that were printed in a Set, and then only handle words that are not yet in the set.

    Side remark: though it’s true that one can think about this as a graph problem, your code doesn’t treat this as such. If we were to treat this as a graph problem, then we wouldn’t make the assumption that each word has all its synonyms listed in the corresponding ArrayList, thus calling for the calculation of the symmetric and transitive closure. Only then would we extract the equivalence classes.

    (In reality the synonym relation is not transitive, I know.)

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I think this will work for you: function makeid(length) {… May 12, 2026 at 1:19 pm
  • Editorial Team
    Editorial Team added an answer I swear I spent several hours trying to figure this… May 12, 2026 at 1:19 pm
  • Editorial Team
    Editorial Team added an answer The problem is that the size of Class1 depends on… May 12, 2026 at 1:18 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

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.