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...
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...
in java. using the following template as a method, public static void shellSort(int[] array) { create...
in java. using the following template as a method, public static void shellSort(int[] array) { create a program that counts the number of comparisons and swaps of the sorting method does using int array1[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; // Best Case Test int array2[] = {10, 9, 8, 7, 6, 5, 4, 3, 2, 1}; // Worst Case Test int array3[] = {10, 4, 8, 2, 6, 1, 9, 3, 7, 5}; //...
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.)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT