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()); } }}
Question
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()); } }}
Solution
The output of the Java code will be the names sorted in descending order based on the associated integer values. The names are "John", "Anne", and "Bob". The code creates a HashMap with names as keys and integers as values, then it creates a Set of the entries in the Map. This Set is converted into a List, which is then sorted in descending order based on the values associated with each key. The sorted keys are then printed out. So, the output will be:
John Anne Bob
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
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); } }
What is the output of the following program? public class Test { public static void main(String[] args) { int[][] values = {{3, 4, 5, 1 }, {33, 6, 1, 2}}; for (int row = 0; row < values.length; row++) { java.util.Arrays.sort(values[row]); for (int column = 0; column < values[row].length; column++) System.out.print(values[row][column] + " "); System.out.println(); } } } Group of answer choicesThe program prints two rows 3 4 5 1 followed by 33 6 1 2.The program prints on row 3 4 5 1 33 6 1 2.The program prints two rows 3 4 5 1 followed by 2 1 6 33.The program prints two rows 1 3 4 5 followed by 1 2 6 33.The program prints one row 1 3 4 5 1 2 6 33.
What is the output of the following code snippet?ArrayList<String> names = new ArrayList<>();names.add("John");names.add("Jane");System.out.println(names.size());Question 14Answera.0b.1c.2d.Error
What is the output of the following program:import java.util.*;public class linked_list { public static void main(String args[]) { LinkedList<String> ll = new LinkedList<String>(); ll.add("Mercedes"); ll.add("Audi"); ll.add("BMW"); Iterator i = ll.descendingIterator(); while (i.hasNext()) { System.out.println(i.next()); } }}Select one:a.BMWAudiMercedesb.AudiMercedesBMWc.MercedesBMWAudid.MercedesAudiBMW
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.