Topics Index

A Generic Class With Two Type Parameters2773

If you need explanation Read this topic

If you need Answer Take test on this topic

A Generic Class With Two Type Parameters2773
What will be the output of the following program?
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class Test {
    public static void main(String args[]){  
        Map<Integer,String> map = new HashMap<Integer,String>();  
        Set<Map.Entry<Integer,String>> set = map.entrySet();  
        Iterator<Map.Entry<Integer,String>> itr = set.iterator();  
            Map.Entry e =;
            System.out.println(e.getKey()+" "+e.getValue());  

A. 1 Merit
2 Campus
3 Java
4 School
B. 1 Merit
3 Java
2 Campus
4 School
C. Compilation Error
D. Runtime Error
Topic: A Generic Class With Two Type Parameters In Java

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 a.sop prints depends on key value in the order of 1,2,3 4

Posted by Maheshwari Natarajan    2014-11-04 12:37:36

ans is A as map. put(K key, V value) method is used to associate the specified value with the specified key in this map. itr.hasNext()  is condition statement it is executed if it returns true here getKey()
Returns the key corresponding to this entry.getValue() Returns the value corresponding to this prints 1 Merit 2 Campus 3 java 4 School

Posted by Ramya Reddy Addulla    2014-11-04 12:51:06

Ans is A...HashMap stores the data in the form of the key pair value...and hashmap is HashMap keys must be unique and values can does not provide insertion order and this wil allow th null insertion....we have fill ratio which means when ever an hashmap fill specifed amount of ration agian hashmpa is created with new size based on formula...

put() is used to insert keys and values...get is used to getKey() is used to get the key...

here we have hashmap with Integer and String objects...
EntrySet: Returns a Set view of the mappings contained in this map.

here we use the iterator which is an universal cursor which can b applicable for all collection classes...using this we can add() remove() every time we are accessing the key and value....

Posted by Uday Kumar    2014-11-04 13:30:33


Posted by Vanaja Vannu    2014-11-04 14:34:56

This dose is now closed and the winners are Ramya Reddy Addulla,  for 'First Correct Comment', Uday Kumar,  for 'Best Comment' and Sai Ram for the 'Popular Comment'. The 'lucky liker' is Uday Kumar. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-11-05 01:39:11

© meritcampus 2019

All Rights Reserved.

Open In App