java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow java.util arrow How to use of HashMap
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4086
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to use of HashMap E-mail
User Rating: / 1296
PoorBest 

Map is an object that stores key/volume pairs. Given a key, you can find its value. Keys must be unique, but values may be duplicated. The HashMap class provides the primary implementation of the map interface. The HashMap class uses a hash table to implementation of the map interface. This allows the execution time of basic operations, such as get() and put() to be constant.

This code shows the use of HaspMap. In this program HashMap maps the names to account balances.

import java.util.*;

public class HashMapDemo {

  public static void main(String[] args) {

    HashMap hm = new HashMap();
    hm.put("Rohit"new Double(3434.34));
    hm.put("Mohit"new Double(123.22));
    hm.put("Ashish"new Double(1200.34));
    hm.put("Khariwal"new Double(99.34));
    hm.put("Pankaj"new Double(-19.34));
    Set set = hm.entrySet();

    Iterator i = set.iterator();

    while(i.hasNext()){
      Map.Entry me = (Map.Entry)i.next();
      System.out.println(me.getKey() " : " + me.getValue() );
    }

    //deposit into Rohit's Account
    double balance = ((Double)hm.get("Rohit")).doubleValue();
    hm.put("Rohit"new Double(balance + 1000));

    System.out.println("Rohit new balance : " + hm.get("Rohit"));

  }
}

Output Screen:

Rohit : 3434.34
Ashish : 1200.34
Pankaj : -19.34
Mohit : 123.22
Khariwal : 99.34
Rohit new balance : 4434.34

 Related Tips

 
< Prev   Next >
 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.