Ankit_Add

Sunday, January 4, 2015

Hash Map in Core Java

Hash Map is a widely used data structure in core java. Many of the programmers widely used this in programming.

Here is a simple program of Hash Map

In this program you able to learn
1) How to crate a Hash Map
2)How to put data in Hash Map
3)How to retrieve data from Hash Map
4)How to use Hash Map data in your application



import java.util.*;

public class HashMapDemo {

   public static void main(String args[]) {
  
      // Create a hash map
      HashMap hm = new HashMap();
      // Put elements to the map
      hm.put("Mark", new Double(3434.34));
      hm.put("Bill", new Double(123.22));
      hm.put("Janny", new Double(1378.00));
      hm.put("Daisy", new Double(99.22));
      hm.put("Tom", new Double(-19.08));
     
      // Get a set of the entries
      Set set = hm.entrySet();
      // Get an iterator
      Iterator i = set.iterator();
      // Display elements
      while(i.hasNext()) {
         Map.Entry me = (Map.Entry)i.next();
         System.out.print(me.getKey() + ": ");
         System.out.println(me.getValue());
      }
      System.out.println();
      // Deposit 1000 into Marks's account
      double balance = ((Double)hm.get("Mark")).doubleValue();
      hm.put("Mark", new Double(balance + 1000));
      System.out.println("Mark's new balance: " +
      hm.get("Mark"));
   }
}

No comments:

Post a Comment