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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T18:45:47+00:00 2026-06-03T18:45:47+00:00

I have to do a synonyms dictionary using a TreeMap. The TreeMap is of

  • 0

I have to do a synonyms dictionary using a TreeMap. The TreeMap is of <Word, ArrayList<Word>> type. That means that for each key represented by a Word there will be a list of synonyms. When I want to list the contents of the dictionary, by using the method below, I discover that the ArrayList returned is null. What can I do? I tried tracing the code but I don’t seem to find the error.
The method is :

public String listContent() {
    Set set = wordList.keySet();
    Iterator it = set.iterator();
    String result = new String();
    ArrayList<Word> words = new ArrayList<Word>();
    while (it.hasNext()) {
        Word temp = (Word) it.next();
        words = wordList.get(temp);
        if (words != null) {
            Iterator it2 = words.iterator();
            result += temp.getContent();
            result += " - ";
            int size = words.size();
            while (it2.hasNext()) {
                result += ((Word) it2.next()).getContent();
                if (size != 1)
                    result += ", ";
                size--;
            }
            result += "\n";
        }
    }
    return result;
}

The ArrayList returned by wordList.get(temp) is null for some of the inserted elements. I checked the watches but there, they’re not. What should I do ?

wordList is a TreeMap<Word, ArrayList<Word>>;

EDIT – the addWord method

public void addWord(String content1, String content2)
{
  Word w1 = new Word(content1);
  Word w2 = new Word(content2);
  Set set = wordList.entrySet();
  Iterator it = set.iterator();
  boolean ok=false;
  while(it.hasNext())
  {
    Map.Entry<Word,ArrayList<Word>> temp = (Map.Entry<Word,ArrayList<Word>>) it.next();
    if(temp.getKey().getContent().matches(content1))
    {
      ArrayList<Word> words = temp.getValue();
      Iterator it2 = words.iterator();
      if(words.isEmpty()) words.add(w2);
      else
      {
        boolean ok2=true;
        while(it2.hasNext())
        {
          Word tempy = (Word) it2.next();
          if(tempy.getContent().equals(content2))
          {
            ok2=false;
            break;
          }
        }
        if(ok2) words.add(w2);
      }
      ok=true;
    }
  }
  if(!ok) {
    ArrayList<Word> tempys = new ArrayList<Word>();
    tempys.add(w2);
    wordList.put(w1,tempys);
  }

}

EDIT 2 – Word Class

   public class Word implements Serializable,Comparable {

private String content;

public Word (String content)
{
    this.content = content;
}

public void setContent(String content)
{
    this.content=content;
}

public String getContent()
{
    return content;
}

@Override
public int compareTo(Object o) {
    if(((Word)o).getContent().equals(this.getContent())) return 0;
    return 1;
}

}
  • 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-03T18:45:48+00:00Added an answer on June 3, 2026 at 6:45 pm

    Your compareTo method is wrong. The contract is that if A > B, then you must have B < A. Your implementation always returns 1 if the contents are not equal.

    You should implement it like this:

    @Override
    public int compareTo(Word w) {
        return this.content.compareTo(w.content);
    }
    

    (and the Word class should implement Comparable<Word>, not Comparable).

    Since a TreeMap uses this method to tell if some word is bigger or smaller than another one, and since the method returns incoherent results, the Map also returns incoherent results.

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

Sidebar

Related Questions

I have a dictionary of about 50,000 words; each word has-many synonyms, antonyms, etc.
We have a couple of synonyms that are being used to reference tables in
I am working on a simple dictionary where I have words and synonyms to
I have a huge library file containing a word and it's synonyms, this is
I have a Word model for a dictionary/thesaurus: public class Word { public virtual
i would like to have a query that will solve my problem in native
I have a query that I am generating the XML from in Oracle using
I am making a synonyms list that I will store it in database and
I am trying to re-write a dictionary program in python(3.X). I have been using
Have a SomeLib.pro file that contains: CONFIG += debug TEMPLATE = lib TARGET =

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.