Friday, 11 September 2015

HashTable in java util package

import java.util.*;
class HashtableDemo1
{ public static void main(String args[])
{ // Create a hash map.
Hashtable<String, Double> ht = new Hashtable<String, Double>();
      // Put elements to the map
ht.put("Devesh",1000.00);
ht.put("Rahul",1500.00);
ht.put("Rakesh",1200.00);
ht.put("Manohar",900.00);
ht.put("Prateek",1300.00);
// Get a set of the entries.
Set<String> set = ht.keySet();
// Display the set.
// Get an iterator.
Iterator <String>itr = set.iterator();
String name;
while(itr.hasNext())
{ name = itr.next();
System.out.println(name + ": " + ht.get(name));
}
System.out.println();
  // Deposit 1000 into Prateek's account.
double balance = (Double) ht.get("Prateek");
ht.put("Prateek", balance + 1000);
balance = (Double)ht.get("Prateek");
System.out.println("Prateek's new balance: " + balance);
}
}

No comments:

Post a Comment