import java.util.*; public class Test { public static void main(String[] args) { Map<Set<String>, List<String>> m = new HashMap<>(); ArrayList<String> list1 = new ArrayList<>(); list1.add("30"); list1.add("40"); ArrayList<String> list2 = new ArrayList<String>(); list2.add("35"); list2.add("45"); Set<String> set1 = new HashSet<>(); set1.add("ravi"); set1.add("ravi"); m.put(set1,list1); m.put(set1,list2); System.out.println(m); } }
Question
import java.util.*; public class Test { public static void main(String[] args) { Map<Set<String>, List<String>> m = new HashMap<>(); ArrayList<String> list1 = new ArrayList<>(); list1.add("30"); list1.add("40"); ArrayList<String> list2 = new ArrayList<String>(); list2.add("35"); list2.add("45"); Set<String> set1 = new HashSet<>(); set1.add("ravi"); set1.add("ravi"); m.put(set1,list1); m.put(set1,list2); System.out.println(m); } }
Solution
The given code is written in Java. It imports the java.util package and defines a class called Test. Inside the Test class, there is a main method which serves as the entry point of the program.
The program creates a Map object called m with keys of type Set<String> and values of type List<String>. It then creates two ArrayList objects called list1 and list2, and adds some elements to them.
Next, it creates a Set object called set1 and adds some elements to it. The elements in a set are unique, so even though "ravi" is added twice, it will only be stored once in the set.
The program then puts set1 as the key and list1 as the value in the map m using the put method. It does the same for set1 and list2, effectively replacing the previous value associated with set1.
Finally, the program prints the contents of the map m using the println method.
Similar Questions
What is the output of the following code?import java.util.*;public class Main { public static void main(String[] args) { Map map = new HashMap<>(); map.put("A", 1); map.put("B", 2); map.put("C", 3); for (Map.Entry entry : map.entrySet()) { if (entry.getValue() == 2) { map.remove(entry.getKey()); } } System.out.println(map); }}a){A=1, C=3}b)IllegalArgumentExceptionc){A=1, B=2, C=3}d){B=2, A=1, C=3}e)ConcurrentModificationException
What will be the output of the following Java code?Code:import java.util.*;import java.util.Map.Entry;public class OrderValue{ public static void main(String a[]){ Map<String, Integer> names = new HashMap<String, Integer>(); names.put("Anne", 10); names.put("John", 35); names.put("Bob", 2); Set<Entry<String, Integer>> set = names.entrySet(); List<Entry<String, Integer>> list = new ArrayList<Entry<String, Integer>>(set); Collections.sort( list, new Comparator<Map.Entry<String, Integer>>() { public int compare( Map.Entry<String, Integer> order1, Map.Entry<String, Integer> order2) { return (order2.getValue()).compareTo( order1.getValue() ); } }); for(Map.Entry<String, Integer> entry:list){ System.out.print(" "+entry.getKey()); } }}
public class tenquantrongdenthesao { Map<String, double> cayvl =new HashMap<String, double>(); private ArrayList<String> d1 = new ArrayList<>(); private ArrayList<String> d2 = new ArrayList<>(); private String ten; private String maso; public tenquantrongdenthesao(String ten, String maso) { this.ten = ten; this.maso = maso; }
import java.util.*;class TestJavaCollection1 { public static void main(String args[]) { ArrayList<String> list = new ArrayList<String>(); // Creating arraylist list.add("Apple"); // Adding object in arraylist list.add("Orange"); list.add("Strawberry"); list.add("Apple"); // Traversing list through Iterator Iterator itr = list.iterator(); while (itr.hasNext()) { System.out.println(itr.next()); } }}Select one:a.AppleOrangeStrawberryAppleb. No outputc.AppleOrangeStrawberryd. Compilation error
import java.util.ArrayList;class Main { public static void main(String[] args){ ArrayList<String> animals = new ArrayList<>(); animals.add("Dog"); animals.add("Cat"); animals.add("Horse"); animals.remove(2); System.out.println("ArrayList: " + animals); }}Select one:a.ArrayList: [Dog, Cat, Horse]b.ArrayList: [Horse]c.ArrayList: [Dog]d.ArrayList: [Dog, Cat]
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.