Question

In: Computer Science

In java. Write a method static <K, V> void addToMultiMap(Map<K, Set<V>> map, K key, V value)....

In java.

Write a method static <K, V> void addToMultiMap(Map<K, Set<V>> map, K key, V value). addToMultiMap must add the value, if present, to the set associated with the given key, creating the set if necessary. You may assume all keys already in the map are associated with non-null values.

For full credit, your method must include generic types, and must not contain unnecessary method calls or loops, even if they do not otherwise impact correctness. You may assume Map, Set, HashMap, and HashSet are correctly imported from java.util. You may not import other classes.

Solutions

Expert Solution

A generic method is a method which can take any type of argument and perform a certain operation(which is adding to the map in this case).

Kindly find belwo the generic method addToMultiMap(Map<K, Set<V>> map, K key, V value):

import java.util.*;

public class MultiMap {

    public static <K, V> void addToMultiMap(Map<K, Set<V>> map, K key, V value){
        Set<V> set = new HashSet<>();
        if (map.get(key) != null)
            set = map.get(key);
        set.add(value);

        map.put(key, set);
    }

    public static void main(String[] args){
        Map<Integer, Set<String>> map = new HashMap<>();
        addToMultiMap(map, 1, "kapil");
        addToMultiMap(map, 2, "raman");
        addToMultiMap(map, 3, "gaveesh");
        addToMultiMap(map, 1, "kapil");

        System.out.println(map);

        Map<Integer, Set<Integer>> mapTwo = new HashMap<>();
        addToMultiMap(mapTwo, 1, 11);
        addToMultiMap(mapTwo, 2, 22);
        addToMultiMap(mapTwo, 3, 33);
        addToMultiMap(mapTwo, 1, 11);

        System.out.println(mapTwo);
    }
}

Output of above program is as follows:

{1=[kapil], 2=[raman], 3=[gaveesh]}
{1=[11], 2=[22], 3=[33]}


Related Solutions

JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveSelectionSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA please write this method public static void recursiveMergeSort(int[] arr) { }
JAVA Given the header of a method public static void m1 (int[ ] max) Write down...
JAVA Given the header of a method public static void m1 (int[ ] max) Write down Java codes to invoke m1 method, declare variables as needed, (Do NOT implement the method)
Name the project pa3 [Method 1] In the Main class, write a static void method to...
Name the project pa3 [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. [Method 2]...
3. [Method 1] In the Main class, write a static void method to print the following...
3. [Method 1] In the Main class, write a static void method to print the following text by making use of a loop. Solutions without a loop will receive no credit. 1: All work and no play makes Jack a dull boy. 2: All work and no play makes Jack a dull boy. 3: All work and no play makes Jack a dull boy. 4: All work and no play makes Jack a dull boy. 4. [Method 2] In the...
For the division method for creating hash functions, map a key k into one of m...
For the division method for creating hash functions, map a key k into one of m slots by taking the remainder of k divided by m. The hash function is:        h(k) = k mod m,     where m should not be a power of 2. For the Multiplication method for creating hash functions,     The hash function is h(k) = └ m(kA –└ k A ┘) ┘ = └ m(k A mod 1) ┘    where “k A...
write a java program that allows main to work public static void main(String[] args) { /*...
write a java program that allows main to work public static void main(String[] args) { /* Start with the empty list. */ LinkedList list = newLinkedList(); // // ******INSERTION****** // // Insert the values deleteFront(list); deleteBack(list); list = insert(list, 1); list = insert(list, 2); list = insert(list, 3); list = insert(list, 4); list = insert(list, 5); list = insert(list, 6); list = insert(list, 7); list = insert(list, 8);    // Basic Operations on the LinkedList printList(list); insertFront(list,0); printList(list); insertBack(list,999); printList(list);...
FOR JAVA: Need to write a code for the implementation of this public static method: findLongestPalindrome...
FOR JAVA: Need to write a code for the implementation of this public static method: findLongestPalindrome takes a Scanner scn as its parameter and returns a String. It returns the longest token from scn that is a palindrome (if one exists) or the empty string (otherwise). (Implementation note: You'll find your isPalindrome method helpful here. This method calls for an optimization loop.)
Write a method public static void minMax(int[] arr) that takes an array of unique ints of...
Write a method public static void minMax(int[] arr) that takes an array of unique ints of length at least two as an argument, and swaps the smallest value of the array into the 0th position and swaps the largest value of the array into the last position. For example, if int[] a = {4, 3, 2, 6, 1, 5}, the method call minMax(a) should modify the array so that it is {1, 3, 2, 5, 4, 6}. The method should...
Write a static method remove(int v, int[] in) that will return a new array of the...
Write a static method remove(int v, int[] in) that will return a new array of the integers in the given array, but with the value v removed. For example, if v is 3 and in contains 0, 1, 3, 2, 3, 0, 3, and 1, the method will return an array containing 0, 1, 2, 0, and 1. Hint: You can follow two steps to solve this problem: Create an array in the method, let say you called it result....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT