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.
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.
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,...
Word to Digit Programming challenge description: Given a string representation of a set of numbers, print...
Word to Digit Programming challenge description: Given a string representation of a set of numbers, print the digit representation of the numbers. Input: Your program should read lines from standard input. Each line contains a list of word representations of numbers separated by a semicolon. There are up to 20 numbers in one line. The numbers are "zero" through "nine". Output: Print the sequence of digits. Test 1 Input zero;two;five;seven;eight;four Expected Test 1 output 025784 Test 2 Input three;seven;eight;nine;two Expected...
// (A) Implement the code below. For a given graph, return a Set of type T...
// (A) Implement the code below. For a given graph, return a Set of type T of all of the strongly connected nodes of the given key. public Set<T> stronglyConnectedComponent(T key) { return null; } // (B) Implement the code below. Optimally find the shortest path between the given start and end node. public List<T> shortestPath(T startLabel, T endLabel) {        return null; }
Given the following information, what is the historical real return for long-term government bonds? Long-term government  ...
Given the following information, what is the historical real return for long-term government bonds? Long-term government   6.40% Long-term corporate 6.50% Inflation rate 3.15% (A) 9.75% (B) 3.25% (C) 3.15% (D) -3.36% Consider the following information about two stocks and indicate which stock has the most systematic risk. State Probability Stock A Stock B Recession   0.15 0.11 (0.35) Norman 0.55 0.18 0.11 Boom 0.30 0.08 0.31 Market risk premium      8.50% Risk-free rate    3.00% (A) Both risk are the same...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT