Example - HashMap
6 July 2008Map 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("A", new Double(3434.34)); hm.put("B", new Double(123.22)); hm.put("C", new Double(1200.34)); hm.put("D", new Double(99.34)); hm.put("E", 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 A's Account double balance = ((Double)hm.get("A")).doubleValue(); hm.put("A", new Double(balance + 1000)); System.out.println("A new balance : " + hm.get("Rohit")); } }
Output Screen:
A : 3434.34 B : 1200.34 C : -19.34 D : 123.22 E : 99.34 A new balance : 4434.34
Related Posts:
Top Of Page | Trackback
If you found this page useful, consider linking to it. Simply copy and paste the code below into your web site.
It will look like this: Example - HashMap
What ( if any ) differences between Hashtable / HasMap / TreeMap other than Keys for TreeMap are kept ordered and they changed the Name from table to Map. I would rather not split nuances with masters but I am designing the principal session master object for a web transaction Servlet. To do this, I have to dig past the simplified front end we are always told to use. A week ago, I got NetBeans and other than the semi-colon where I left off in mid-statement to install it, it found nothing to complain about in 32,000 keystrokes. I have it templated the SessionManagerObject as a TreeMap, keying sessions on a SecureRandom Integer. I will then store what I discoved this morning ( while reading Marty Hall’s book ) is a Bean: The OrderFormRequestObject is a customer, trasaction in progress before shipping any electronic transactions to a site that is capable of verifiying an electronic transaction.
Fail Resistance and robust synchronization are near-critical, ordering does not matter as long as keyExists(Integer) and getOjbect((Integer)Key) may be determined with good response and threading.
Its worth noting that Java 1.4 is EOL. So you could be using Java 5 syntax. Here is an example which perhaps clearer.
public class HashMapDemo {
public static void main(String[] args) {
// use generic where approriate.
Map hm = new HashMap();
// use autoboxing.
hm.put(”A”, 3434.34);
hm.put(”B”, 123.22);
hm.put(”C”, 1200.34);
hm.put(”D”, 99.34);
hm.put(”E”, -19.34);
// use the foreach loop.
for (Entry stringDoubleEntry : hm.entrySet())
System.out.println(stringDoubleEntry.getKey() + ” : ” + stringDoubleEntry.getValue());
//deposit into A’s Account
hm.put(”A”, hm.get(”A”) + 1000);
System.out.println(”A new balance : ” + hm.get(”A”));
}
}
It looks like the blog strips generics.
public class HashMapDemo {
public static void main(String[] args) {
// use generic where approriate.
Map<String, Double< hm = new HashMap();
// use autoboxing.
hm.put(”A”, 3434.34);
hm.put(”B”, 123.22);
hm.put(”C”, 1200.34);
hm.put(”D”, 99.34);
hm.put(”E”, -19.34);
// use the foreach loop.
for (Entry<String, Double> stringDoubleEntry : hm.entrySet())
System.out.println(stringDoubleEntry.getKey() + ” : ” + stringDoubleEntry.getValue());
//deposit into A’s Account
hm.put(”A”, hm.get(”A”) + 1000);
System.out.println(”A new balance : ” + hm.get(”A”));
}
}