Menu
Topics Index
...
`

Numbers in Hashtable1157


If you need explanation Read this topic

If you need Answer Take test on this topic

Numbers in Hashtable1157
What will be the output of the following program?
import java.util.*;
import java.util.Map.*;

public class HashTable
{
    public static void main(String args[])
    {
        Hashtable<String, String> hashtable = new Hashtable<String, String>();
        hashtable.put("first", "1");
        hashtable.put("second", "2");
        hashtable.put("third", "3");
        hashtable.put("third", "3");
        hashtable.put("five", "5");
        hashtable.put("six", "5");

        List<String> list = new ArrayList<String>(hashtable.keySet());
        Collections.sort(list);

        for(String key : list)
        {
            System.out.println(key + " -> " + hashtable.get(key));
        }
    }
}

A. first -> 1
second -> 2
third -> 3
six -> 5
B. first -> 1
five -> 5
second -> 2
six -> 5
third -> 3
third -> 3
C. first -> 1
five -> 5
second -> 2
six -> 5
third -> 3
D. first -> 1
second -> 2
third -> 3
third -> 3
five -> 5
six -> 5
E. Compilation Error  or Runtime Error
Topic: Java Hashtable

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.
Hashtable will not store duplicate keys.. so the list will get the following keys {first,second,third,five,six} and then we are sorting the list.. so it will print in ascending order as first, five, second,six,third along with the associated values in hashtable..

Posted by Gopinath Manchikanti    2015-02-02 08:43:33


c

Posted by Shubham Bansal    2015-02-02 11:43:02


i'll go with option c ... reason is like.. when we save values in hashtable.. they will be saved in Random Order and when we pass the keys to ArrayList .. they will be saved in Same Order .. and In Next Step  we have used the Collections.sort which will give the Results in Sorted Order ( Dictionary Order Because of Strings ) ... in Next For Loop .. the Sorted Values of List will be Printed..

Posted by ?????????? ?????    2015-02-02 14:42:50


ANS IS C

--Here,the concept of "HASTABLE" is used.

--six elements are entered in hashtable using put method().

--Hashtable was part of the original java.util and is a concrete implementation of a Dictionary.However, Java 2 re-engineered Hashtable so that it also implements the Map interface.

--Like HashMap, Hashtable stores key/value pairs in a hash table. When using a Hashtable, you specify an object that is used as a key, and the value that you want linked to that key. The key is then hashed, and the resulting hash code is used as the index at which the value is stored within the table.

--After that hashtable is converted into list of strings and sort() method of collections class is used which is used to sort the strings of hashtable aplhabetically.

--The,For loop is used to print the key and the index at which it is present.

Posted by Mânïshå Mùlchåndânï    2015-02-02 17:35:32


This dose is now closed and the winners are Gopinath Manchikanti,  for 'First Correct Comment', ?????????? ?????, Mânïshå Mùlchåndânï,  for 'Best Comment' and Shubham Bansal for the 'Popular Comment'. The 'lucky liker' is Mânïshå Mùlchåndânï. Please login into Merit Campus using facebook, to claim your recharge. Go to http://java.meritcampus.com/earnings to raise the recharge.

Posted by Merit Campus    2015-02-03 02:18:49


© meritcampus 2019

All Rights Reserved.

Open In App