Computer Science, asked by sankaruma1704, 9 months ago

What is the output of this program?*
import java.util.*;
class Maps
public static void main(String args[]) {
HashMap obj = new HashMap();
obj.put("A", new Integer(1));
obj.put("B", new Integer(2));
obj.put("C", new Integer(3));
System.out.println(obj);​

Answers

Answered by anindyaadhikari13
33

Corrected Program:

import java.util.*;

class Maps{

   public static void main(String args[]){

       HashMap obj = new HashMap();

       obj.put("A", new Integer(1));

       obj.put("B", new Integer(2));

       obj.put("C", new Integer(3));

       System.out.println(obj);

   }

}

Output:

{A=1, B=2, C=3}

Explanation:

  1. Line 1: Imports all classes from utility package. This is done so as to use the HashMap class.
  2. Line 2: Class declaration.
  3. Line 3: Main() method declaration.
  4. Line 4: A HashMap object is created.
  5. Line 5-7: Elements are added into the Map using put() method in the form of key-values.
  6. Line 8: The HashMap object is displayed on the screen. The output is generally in the form {key1 = value1, key2 = value 2, ... } So, the output is {A=1, B=2, C=3}
  7. Line 9: End of main()
  8. Line 10: End of class.
Similar questions