Monday, 14 September 2015

TreeMap Class Example 1

import java.util.*;
class TreeMapDemo
{ public static void main(String args[])
{ // Create a hash map.
TreeMap<String, Double> tm = new TreeMap<String, Double>();
tm.put("Devesh",1000.00);
tm.put("Rahul",1500.00);
tm.put("Rakesh",1200.00);
tm.put("Manohar",900.00);
tm.put("Prateek",1300.00);
tm.put("Pawan", null);
//tm.put(null,34.56); will result in run-time error
// Get a set of keys
Set<String> set = tm.keySet();
// Display the set.
for(String key : set)
{
    System.out.print(key + ": ");
System.out.println(tm.get(key));
}
System.out.println();
// Deposit 1000 into Prateek's account.
double balance = tm.get("Prateek");
tm.put("Prateek", balance + 1000);
balance = (Double)tm.get("Prateek");
  System.out.println("Prateek's new balance: " + balance);
}
}

No comments:

Post a Comment