Other 2006 FR Questions FR other years Be Prepared Home
AB-1
Part (a)
  // adds syn to the set of synonyms associated with word
  // in this Thesaurus;
  // if word is not a key in this Thesaurus, adds an
  // entry with word as key and a set containing syn
  // as the associated value
  public void addSynonym(String word, String syn)
  {
    Set<String> synonyms = wordMap.get(word);
    if (synonyms == null)
    {
      synonyms = new TreeSet<String>(); 1
      wordMap.put(word, synonyms);
    }
    synonyms.add(syn); 2
  }
Notes:
  1. Or HashSet.

  2. It is okay to first put synonyms into the map, then add an element to it, because synonyms is a value, not a key.

Part (b)
  // removes the word syn from each synonym set in this Thesaurus;
  // returns a set of the words (keys) whose associated
  // synonym sets originally contained the word syn;
  // if syn was not contained in any synonym set, returns an empty set
  public Set<String> removeSynonym(String syn)
  {
    Set<String> affectedWords = new TreeSet<String>();

    for (String word : wordMap.keySet())
    {
      Set<String> synonyms = wordMap.get(word);
      if (synonyms.contains(syn))
      {
        synonyms.remove(syn);
        affectedWords.add(word);
      }
    }

    return affectedWords;
  }

Other 2006 FR Questions | Back to Contents

Copyright © 2006 by Skylight Publishing
support@skylit.com