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)
In Java, write the method public static void insertUnique(List l, T e), user of the ADT...
In Java, write the method public static void insertUnique(List l, T e), user of the ADT List. The method takes a list l and an element e and inserts the element at the end of the list only if it is not already there. Example 0.2. If l : A → B → C, then after calling insertUnique(l, "C"), the list does not change. Calling insertUnique(l, "D") will make l be : A → B → C → D.
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...
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]...
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...
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...
Re-write this method using iteration ONLY JAVA class Main { public static void merge(int arr[], int...
Re-write this method using iteration ONLY JAVA class Main { public static void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r-m; int left[] = new int[n1]; int right[] = new int[n2]; for (int i = 0; i < n1; ++i) left[i] = arr[l + i]; for (int j = 0; j < n2; ++j) right[j] = arr[m + 1 + j]; int i=0; //left int j=0; //...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT