Question

Answer the following Java Collection/Map Question: The antonym map is a map that associating a word...

Answer the following Java Collection/Map Question:

The antonym map is a map that associating a word (the key) to its antonym. Suppose the antonym map wasn't necessarily symmetric: it might have an entry that maps the key "short" to "tall", but not have an entry that maps the key "tall" to "short": there is no key "tall" in the map. Yet, the antonym relationship certainly is symmetic: if the opposite of "short" is "tall", the opposite of "tall" is "short". Write a code fragment that attempts to find the antonym directly in this map by using it as a key, but if it is not there, attempts to find it as a value, then add it back as a key for a new entry.

For sake of simplicity, we suppose that each word has only one antonym.

Example of an antonym map                             

Big --> Small
Alive --> Dead
Clever --> Stupid

“Stupid” is the value associated to the key “Clever”, the antonym of “Clever”. To retrieve the antonym of “Stupid” which it doesn’t exist in the map, you should add it as a key and the original key becomes the value.                                   

Big --> Small
Alive --> Dead
Clever --> Stupid
Stupid --> Clever

0 0
Add a comment Improve this question Transcribed image text
Answer #1

PFB requested code with appropriate comments:

=================

import java.util.concurrent.ConcurrentHashMap;
import java.util.Iterator;
import java.util.Map;

public class Main
{
// Map to store antonyms
static Map<String, String> antonymMap;
   public static void main(String[] args) {
   antonymMap = new ConcurrentHashMap<String, String>();
       loadMap();
       findAntonym("Stupid");
   }
  
   // Loads the antonymMap with antonyms
   public static Map<String, String> loadMap() {
       antonymMap.put("Big", "Small");
       antonymMap.put("Alive", "Dead");
       antonymMap.put("Clever", "Stupid");
       return antonymMap;
   }
  
   // finds the antonym of the word provided
   public static void findAntonym(String str) {
   // check if given word is a key in the map
   if (antonymMap.containsKey(str)) {
   System.out.println("Antomym of " + str + " is " + antonymMap.get(str));
   }
   // check if given word is a value in the map
   else if (antonymMap.containsValue(str)) {
   Iterator<Map.Entry<String, String>> iterator = antonymMap.entrySet().iterator();
   // iterate over the map and get the key for which value is the word we are looking for
while (iterator.hasNext())
{
Map.Entry<String, String> entry = iterator.next();
if (str.equals(entry.getValue())) {
   System.out.println("Antomym of " + str + " is : " + entry.getKey());
   antonymMap.put(str, entry.getKey());
   System.out.println("updated antonym map :: " + antonymMap);
   break;
   }
}
   } else {
   System.out.println("Antonym of " + str + " is not found in the map");
   }
   }
}

Add a comment
Know the answer?
Add Answer to:
Answer the following Java Collection/Map Question: The antonym map is a map that associating a word...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Answer the following Java Collection/Map Question: The Map interface has a method putAll() that adds all...

    Answer the following Java Collection/Map Question: The Map interface has a method putAll() that adds all entries in map2 into map1. Write your own putAll2 method ( a generic method), given the two maps as parameters the method will add all entries in map2 into map1 and print map1. Show that the method is working in an application class.

  • JAVA 3 PLEASE ANSWER AS MANY QUESTIONS AS POSSIBLE! ONLY 2 QUESTIONS LEFT THIS MONTH!!! Question...

    JAVA 3 PLEASE ANSWER AS MANY QUESTIONS AS POSSIBLE! ONLY 2 QUESTIONS LEFT THIS MONTH!!! Question 12 pts Which is a valid constructor for Thread? Thread ( Runnable r, int priority ); Thread ( Runnable r, String name ); Thread ( int priority ); Thread ( Runnable r, ThreadGroup g ); Flag this Question Question 22 pts What method in the Thread class is responsible for pausing a thread for a specific amount of milliseconds? pause(). sleep(). hang(). kill(). Flag...

  • Using Jupyter, Please answer the following question: Please import alic.txt and cound the number of words...

    Using Jupyter, Please answer the following question: Please import alic.txt and cound the number of words and characters in it. We need to count the number of words in the txt file and find the most repeated one. In the program, first convert all words to lower case and then convert the first character to the upper case. Then we need to do the analysis. We are not interested in the following words: The, A, And, To, It alice.txt file:...

  • Please read the case provided below and answer the following question: In 2007, JetBlue was a...

    Please read the case provided below and answer the following question: In 2007, JetBlue was a booming young airline with a strong reputation for outstanding service. In fact, the low-fare airline referred to itself as a customer service company that just happened to fly planes. But on Valentine's Day 2007, JetBlue was hit by the perfect storm-literally-of events that led to an operational meltdown. One of the most severe storms of the decade covered JetBlue's main hub at New York's...

  • Read the Article posted below, then answer the following questions: Mergers & acquisitions are a major...

    Read the Article posted below, then answer the following questions: Mergers & acquisitions are a major form of corporate diversification strategy, identify and discuss the top three reasons why most (50-60%) of acquisitions fail to create shareholder value. What are the five major components of “CEMEX Way” and why has this approach been so successful in post-acquisition integration? In your opinion, what can other companies learn from the “CEMEX Way” as a benchmark for acquisition management? Article: CEMEX: Globalization "The...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT