Question

In: Computer Science

Given the nested collection that maps each term to a set of strings   Return a string...

Given the nested collection that maps each term to a set of strings  

Return a string of terms that are repeated in all the nested sets

Given :

{apple=[apple BALL carrot, ball !carrot! ,!Dog*&]}

{apple=[apple BALL carrot, ball !carrot! ,!Dog*&], dog=[ball !carrot! ,!Dog*&]}

Return:

[ball !carrot! ,!Dog*&]

Public static String common(Map<String, Set<Sting>> map)

{

}

Solutions

Expert Solution

Ans)

Java program



import java.util.*;

class CommonInSet {

    public static void main(String[] args) {
        
        Set<String> s1 = new HashSet<String>();
        s1.add("apple BALL carrot");
        s1.add("ball !carrot!");
        s1.add("!Dog*&");
        System.out.println("Set 1: " + s1);

        Set<String> s2 = new HashSet<String>();
        s2.add("ball !carrot!");
        s2.add("!Dog*&");
        System.out.println("Set 1: " + s2);

        Map<String, Set<String>> m = new HashMap<String, Set<String>>();
        m.put("apple", s1);
        m.put("dog", s2);
        System.out.println("Map: " + m);

        System.out.println("Finding the common elements: ");
        Set<String> intersectionSet = new HashSet<String>();
        intersectionSet = common(m);
        System.out.println("Common Elements: " + intersectionSet);
    }

    public static Set<String> common(Map<String, Set<String>> map) {
        if (map == null)
            return null;

        Set set = map.entrySet();
        Iterator itr = set.iterator();
        Map.Entry entry = (Map.Entry) itr.next();
        Set<String> intersectionSet = new HashSet<String>();
        intersectionSet = (Set) entry.getValue();

        while (itr.hasNext()) {
            entry = (Map.Entry) itr.next();
            intersectionSet.retainAll((Set) entry.getValue());
        }

        return intersectionSet;
    }
}

Above compile and execute Java program and get output below screen shot here

Output

******************End*****"*****"**************

if your satisfy above answer please give positive rating or upvote

please don't downvote

if any doubts below comment here

Thankyou!


Related Solutions

3. Write a Java program that generates a set of random strings from a given string...
3. Write a Java program that generates a set of random strings from a given string of same length where no character is ever repeated and characters belong to the original string. Examples Input: “Hello World” Output: “World oHlel”
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of...
Given two ArrayLists of Strings (ArrayList<String>), write a Java method to return the higher count of the characters in each ArrayList.  For example, if list1 has strings (“cat, “dog”, “boat”, “elephant”) and list 2 has strings (“bat”, “mat”, “port”, “stigma”), you will return the value 18.  The list 1 has 18 characters in total for all its strings combined and list2 has 16 characters for all of its strings combined.  The higher value is 18. If the character count is the same, you...
Consider the set of strings A = {c,cc,ccc}. What is the shortest string the set of...
Consider the set of strings A = {c,cc,ccc}. What is the shortest string the set of strings A5.
In C: Find a string within a string Given two strings S1 & S2, search for...
In C: Find a string within a string Given two strings S1 & S2, search for an occurrence of the second string within a first string. Note: Do not use system library for the implementation. Input: Code Zinger University Zinger where, First line represents string S1. Second line represents string S2. Output: 5 Here 'Zinger' word starts at 5th index within 'Code Zinger University’. Assume that, The length of strings S1 & S2 are within the range [1 to 10000]....
Given the strings s1 and s2 that are of the same length, create a new string...
Given the strings s1 and s2 that are of the same length, create a new string s3 consisting of the last character of s1 followed by the last character of s2, followed by the second to last character of s1, followed by the second to last character of s2, and so on
Write a function called 'make_triangle(char,n)' that uses a nested loop to return a string that forms...
Write a function called 'make_triangle(char,n)' that uses a nested loop to return a string that forms a triangle. The return string should contain: one instance of char on the first line, two instances of char on the second line, and so on up to n instances of char on line n. Then n-1 instances of char on line n+1, and so on until the triangle is complete. Sample runs of the function looks as follows (this function called from IDLE,...
Suppose that you pick a bit string from the set of all bit strings of length...
Suppose that you pick a bit string from the set of all bit strings of length ten. Find the probability that the bit string has exactly two 1s; the bit string begins and ends with 0; the bit string has the sum of its digits equal to seven; the bit string has more 0s than 1s; the bit string has exactly two 1s, given that the string begins with a 1.
Suppose that you pick a bit string from the set of all bit strings of length...
Suppose that you pick a bit string from the set of all bit strings of length ten. Find the probability that the bit string has exactly two 1s; the bit string begins and ends with 0; the bit string has the sum of its digits equal to seven; the bit string has more 0s than 1s; the bit string has exactly two 1s, given that the string begins with a 1.
double_vowels Given a string, return a copy of the string with all of the vowels doubled....
double_vowels Given a string, return a copy of the string with all of the vowels doubled. Consider the five letters 'aeiou' as vowels. double_vowels('pencil') → 'peenciil' double_vowels('xyzzy') → 'xyzzy' double_vowels('catalog') → 'caataaloog' middle_hash Given a string, do three hash marks '###' appear in the middle of the string? To define middle, we'll say that the number of chars to the left and right of the '###' must differ by at most one. middle_hash('aa###bb') → True middle_hash('aaa###bb') → True middle_hash('a###bbb') →...
(a)Design an algorithm that takes two numeric strings s1 and s2 and return another numeric string...
(a)Design an algorithm that takes two numeric strings s1 and s2 and return another numeric string that represent their sum without using using SHIFT OR any operator +. (b) Analyze time and space of part (a) (c)Implement part (a) in your favorite programming language without using 0+0 operator. You should read the two numeric strings from a FILE input.in and output their addition to standard output. You Solution will be test on 10,000 test cases. Each test case will include...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT