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

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...
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.
Before C++ and classes, strings were stored in simple arrays of characters. The term C-string refers...
Before C++ and classes, strings were stored in simple arrays of characters. The term C-string refers to the classic implementation of strings in the C programming language. A C-string is a sequence of characters terminated by the null character, '\0'. Since C is part of C++, C-string implementations are valid in C++ as well as C. Recall that an array of characters is the underlying data structure for storing C-strings. For example, this definition creates such an array. char myString[100];...
Python Programming: Using re library, and a given string recipe1 return the indications after each ingredient...
Python Programming: Using re library, and a given string recipe1 return the indications after each ingredient '@': for example if recipe1 = "@turkey baked @350degrees" print an output like: [@turkey, @350degrees]
Standing waves are set up on two strings fixed at each end, as shown in the...
Standing waves are set up on two strings fixed at each end, as shown in the drawing. The two strings have the same tension and mass per unit length, but they differ in length by 0.53 cm. The waves on the shorter string propagate with a speed of 41.2 m/s, and the fundamental frequency of the shorter string is 227 Hz. Determine the beat frequency produced by the two standing waves.
Standing waves are set up on two strings fixed at each end, as shown in the...
Standing waves are set up on two strings fixed at each end, as shown in the drawing. The two strings have the same tension and mass per unit length, but they differ in length by 0.54 cm. The waves on the shorter string propagate with a speed of 42.3 m/s, and the fundamental frequency of the shorter string is 239 Hz. Determine the beat frequency produced by the two standing waves.
Writing method in Java Given a string, return the sum of the numbers appearing in the...
Writing method in Java Given a string, return the sum of the numbers appearing in the string, ignoring all other characters. A number is a series of 1 or more digit chars in a row. (Note: Character.isDigit(char) tests if a char is one of the chars '0', '1', .. '9'. Integer.parseInt(string) converts a string to an int.) sumNumbers("abc123xyz") → 123 sumNumbers("aa11b33") → 44 sumNumbers("7 11") → 18
Please implement Sample string toString()method for each class and return itself a string, not the output....
Please implement Sample string toString()method for each class and return itself a string, not the output. import java.util.ArrayList; public class Customer extends User{ private ArrayList orders; public Customer(String display_name, String password, String email) { super(display_name, password, email); } @Override public String getPermissionLevel() { return "CUSTOMER"; } public void addOrder(Order order){ this.orders.add(order); } public ArrayList listOrders(){ return this.orders; }; } ---------------- public class ElectronicProduct extends Product{ private long SNo; private String warranty_period; public ElectronicProduct(long SNo, String warranty_period, String productId, String productName,...
For the following grammars, write the leftmost derivation for the strings given with each. Next to...
For the following grammars, write the leftmost derivation for the strings given with each. Next to each derivation step, write the number of the rule used. Do not combine steps. grammar (6 rules), Σ = {0, 1}: S -> A1B A -> 0A | ε B -> 0B | 1B | ε strings (3): 1, 0011, 01010 grammar (6 rules), Σ = { +, x, a, (, ) } E -> E + T | T T -> T x...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT