Java Hashtable class

Introduction

The following code shows how to use Hashtable class

// Demonstrate a Hashtable. 
import java.util.*; 
  
public class Main { 
  public static void main(String args[]) { 
    Hashtable<String, Double> balance = 
      new Hashtable<String, Double>(); 
 
    Enumeration<String> names; 
    String str; //from   w w  w  .j  av a2s .c o m
    double bal; 
 
    balance.put("HTML", 1234.34); 
    balance.put("CSS", 123.22); 
    balance.put("Java", 1234.00); 
    balance.put("Javascript", 99.22); 
    balance.put("SQL", -19.08); 
 
    // Show all balances in hashtable. 
    names = balance.keys(); 
    while(names.hasMoreElements()) { 
      str = names.nextElement(); 
      System.out.println(str + ": " + 
                         balance.get(str)); 
    } 
 
    System.out.println(); 
 
    // Deposit 1,000 into HTML's account. 
    bal = balance.get("HTML"); 
    balance.put("HTML", bal+1000); 
    System.out.println("HTML's new balance: " + 
                       balance.get("HTML")); 
  } 
}



PreviousNext

Related