Free Data Structures and Algorithms Course









Subscribe below and get all best seller courses for free !!!










OR



Subscribe to all free courses

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 :


 
© 2021 Learn Java by Examples Template by Hubberspot