Pin It

How to use Hashtable in Java with example ?.

Program to demonstrate how to implement Hashtable class in java.util.*; package along with its operations :

package com.hubberspot.collections.example;

import java.util.Hashtable;
import java.util.Enumeration;

public class HashtableExample
{
  public static void main(String args[])
  {
    Hashtable hashtable = new Hashtable();
    String str, name = null;
     
    hashtable.put( "Jonty", 80.2 ); 
    hashtable.put( "Hubberspot", 75.9 );
    hashtable.put( "Java", 105.1 );
    hashtable.put( "Magicman", 65.7 );
    
    System.out.println("Hashtable contains " +
       hashtable.size() + " key value pair."); 

    System.out.println("Retriving all keys from Hashtable");
  
    Enumeration keys = hashtable.keys();
  
    while( keys. hasMoreElements() )
      System.out.println( keys.nextElement() );

    System.out.println("Retriving all values from Hashtable");
  
    Enumeration values = hashtable.elements();
  
    while( values. hasMoreElements() )
      System.out.println( values.nextElement() );
  
    double maximum = 0;
    keys = hashtable.keys();
    values = hashtable.elements();
  
    while( values. hasMoreElements() )
    {
      double percentage = ((Double)values.nextElement())
                 .doubleValue();
      
      str = (String)keys.nextElement();
   
      if(percentage > maximum)
      {
 maximum = percentage;
 name = str;
      }
    }

    System.out.println("Name = " + name);  
    System.out.println("Percentage = " + maximum);  
  }
}


Output of the program :


Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot