Menu
Topics Index
...
`

HashMap


If you need explanation Read this topic

If you need Answer Take test on this topic

HashMap
What will be the output of the following program?
import java.util.*;
import java.util.Map.*;
public class Participate {
    public static void main(String args[]) {
        HashMap participant = new HashMap();
        participant.put(1 + 1, "Samantha");
        participant.put(0 + 1, "Kiran");
        participant.put(2 + 1, "Anushka");
        Set set = participant.entrySet();
        Iterator itr = set.iterator();
        while (itr.hasNext()) {
            Map.Entry m = (Entry) itr.next();
            System.out.print(m.getKey() + " " + m.getValue() + ", ");
        }
        System.out.println();
        itr = set.iterator();
        while (itr.hasNext()) {
            Map.Entry m = (Map.Entry) itr.next();
            System.out.print(m.getKey() + " " + m.getValue() + ", ");
        }
    }
}

A. 2 Samantha, 1 Kiran, 3 Anushka,
B. 1 Kiran, 2 Samantha, 3 Anushka,
C. 2 Samantha, 1 Kiran, 3 Anushka,
2 Samantha, 1 Kiran, 3 Anushka,
D. 1 Kiran, 2 Samantha, 3 Anushka, 
1 Kiran, 2 Samantha, 3 Anushka,
E. Some other output
F. Compilation Error or Runtime Error
Topic: Java HashMap Implementation

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

Ans is C.. hash map= 2->Samantha , 1->Kiran 3->Anushka..we are creating hashmap here and adding elements and then printing the elemens in it twice..

Posted by Om Prakash Om    2014-01-02 08:50:50


E) some other output as hashmap retrieval order is not guaranteed

Posted by Shashanka Mogaliraju    2014-01-02 10:52:45


Answer is D. hashmap maps the variables to values,so values r taken in sorted ordered. So the key 1 is kiran so ths is printed 1st..thn 2 is samantha..3 is anushka...!! and again when we are printing it prints it 1nc agn.

Posted by Shaik Nyamath    2014-01-02 13:31:37


Congratulations Shaik Nyamath. You are this dose winner. We will send you the link using which you can claim your recharge.

Posted by Merit Campus    2014-01-03 04:12:46


© meritcampus 2019

All Rights Reserved.

Open In App