Question
Asked Dec 18, 2019
20 views

Write a method called flipMap() that takes in a map from string keys to string values, and returns a "reverse" or "inverse" version of the same map contents, mapping values from the first map to keys from the first map. For example, if the map provided has key "hello" mapped to value "world", then the map returned by your method should contain key "world" mapping to value "hello". You can modify the provided map, or create a new map.

check_circle

Expert Answer

Step 1

Program approach-

  • Import the essential java packages
  • Define the main function.
  • Declare the variables and their data types accordingly.
  • Assign values to all the four variables w, x,y and z.
  • Use for each loop to iterate over the string values.
  • Define the changeMap method
  • Use inverse method to inverse the given values.

Maps are suitable for mapping key-value connections such as dictionaries. The maps are used for key-based lookups or when someone needs to retrieve and update key-based items.To implement maps in java there are two interfaces: Map and SortedMap, and three classes: TreeMap, LinkedHashMap and HashMap.

 

Step 2

Code(in java)-

//importing the java packages

import java.util.*;

//declaring the main class

public class Main

{

//defining the main method

public static void main (String[]args)

{

//implementing map by using HashMap method

Map < String, String > aa = new HashMap < String, String > ();

//initializing values to all the variables

aa.put ("w", new String ("200"));

aa.put ("x", new String ("400"));

aa.put ("y", new String ("600"));

aa.put ("z", new String ("800"));

System.out.println("Before Mapping");

//using for each loop to iterate over the keys and values

for (Map.Entry < String, String > ent:aa.entrySet ())

{

    //Displaying the keys and values before mapping

System.out.print...

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Programing Language

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Trace the execution of binary search for key=72  showing each complete pass on the following number ...

A: Answer:Finding the position of binary tree key = 72:

question_answer

Q: Why am I getting a error message when trying to INSERT data for the table??   (photo attached)

A: The syntax to insert data in the table isINSERT INTO Table_Name (field_Name1, field_Name2, field_Nam...

question_answer

Q: 1. Explain what the CPU should do when an interrupt occurs. Include in your answer the method the CP...

A: Click to see the answer

question_answer

Q: Provide JavaScript code to change the source of the first inline image in the document to introlmg.p...

A: Program Instructions:For a given inline image with an ID, document can be used to change its source....

question_answer

Q: Create a query that uses any THREE of the tables (SCHOOL, DEPARTMENT, PROFESSOR, TECHNOLOGY &amp; CO...

A: Writing a query based on the given tables by using arithmetic and special operators in the condition...

question_answer

Q: write a program that asks the user how many times to roll a die. The output should include the total...

A: Program Approach-Import all the essential java packages.Declare a class named Main.Define the main m...

question_answer

Q: In c++  I have this working code that requres the user to input 6 times  I was wondering how would i...

A: In this C++ program,Linear search: num is matched with each element of array starting from index 0 u...

question_answer

Q: You are asked to write a simple C program that will accept an integer value in the range of 5-95 and...

A: Program:/*Header files*/#include &lt;stdio.h&gt;#include&lt;stdlib.h&gt;/* Funtion for taking user u...

question_answer

Q: Perform this binary division and find the remainder: 10111101 / 1001   If possible can you help unde...

A: Binary NumberThe number system which has a base- 2 is called binary number system. It has only 2 sym...