Question

Language: JavaScript Create a public class called Inverter with a single class method named invert. invert...

Language: JavaScript

Create a public class called Inverter with a single class method named invert. invert should accept a Map<String, Integer> and return a Map<Integer, String> with all of the key-value pairs swapped. You can assert that the passed map is not null.

Because values are not necessarily unique in a map, normally you would need a way to determine what the right approach is when two keys map to the same value. However, the maps passed to your function will not have any duplicate values.

Below is my current code.

import java.util.Map;
public class Inverter {
public static Map<Integer, String> invert(Map<String, Integer> map) {
assert(!(map.equals(null)));
}
}

Homework Answers

Answer #1

Implementation of Class with Method with Map as arguments and to get return type as Map with key pair being Swapped

As per Map Interface, Keys will be unique but not the values. We can make use of HashMap for storing two keys to store up the same value. Following up on the assertion technique to check NULL arguments, the below code results with Map with key pair being swapped.

The below invert method handles the duplicate issue while swapping the Map key pair values using the Merge function where the values can be replace or we can maintain with the old value itself.

​
   public static class Inverter {
        public Map<Integer, String> invert(Map<String, Integer> map) {
            assert(!(map.equals(null)));
                Map<Integer, String> invertedMap = new HashMap<>();
                invertedMap = map.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey, (oldValue, newValue) -> newValue));
                System.out.println("Inverted Map :: " + invertedMap);
                return invertedMap;
        }
    }

​


Know the answer?
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
Similar Questions
This laboratory assignment involves implementing a data structure called a map. A map associates objects called...
This laboratory assignment involves implementing a data structure called a map. A map associates objects called keys with other objects called values. It is implemented as a Java class that uses arrays internally. 1. Theory. A map is a set of key-value pairs. Each key is said to be associated with its corresponding value, so there is at most one pair in the set with a given key. You can perform the following operations on maps. You can test if...
import java.util.Scanner; import java.util.Random; public class ScoreDice { private Random r; public ScoreDice(long seed) { r...
import java.util.Scanner; import java.util.Random; public class ScoreDice { private Random r; public ScoreDice(long seed) { r = new Random(seed); } public int rollD6() { return r.nextInt(6) + 1; } public int score() { int roll1 = rollD6(); int roll2 = rollD6(); return scoreWithNumbers(roll1, roll2); }    // You will need to write a static method named scoreWithNumbers. // scoreWithNumbers returns a score based on the values // of its inputs, as such: // // - If both inputs are 1...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately difficult problem. Program Description: This project will alter the EmployeeManager to add a search feature, allowing the user to find an Employee by a substring of their name. This will be done by implementing the Rabin-Karp algorithm. A total of seven classes are required. Employee (From previous assignment) HourlyEmployee (From previous assignment) SalaryEmployee (From previous assignment) CommissionEmployee (From previous assignment) EmployeeManager (Altered from previous...
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class....
Complete the redblacktree in Java. Add a public boolean isBlack field to the Node inner class. Make every Node object have a false isBlack field, all new node is red by default. In the end of the insert method, set the root node of your red black tree to be black. Implement the rotate() and recolor() functions, and create tests for them in a separate class. import java.util.LinkedList; public class BinarySearchTree<T extends Comparable<T>> { protected static class Node<T> { public...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
Subject- ( App Development for Web) ( language C#, software -visual studio) Exact question is-Firstly the...
Subject- ( App Development for Web) ( language C#, software -visual studio) Exact question is-Firstly the console calculator is created which perform multiply,divide,sub and add, operation and it accept all type of data (divide by 0 case as well ).Now the main motive is to create the library project from the console calculator project .Than we have to create a unit test project which test the functionality of added library.Make test like Test 1. multiply two positive number,Test 2. Add...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT