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
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:
- Line 1: Imports all classes from utility package. This is done so as to use the HashMap class.
- Line 2: Class declaration.
- Line 3: Main() method declaration.
- Line 4: A HashMap object is created.
- Line 5-7: Elements are added into the Map using put() method in the form of key-values.
- 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}
- Line 9: End of main()
- Line 10: End of class.
Similar questions
Accountancy,
4 months ago
English,
4 months ago
Business Studies,
4 months ago
Computer Science,
9 months ago
World Languages,
1 year ago
Math,
1 year ago