iForeRunner
 
   
 
  JAVA  
   
   
   
 
  Java |Util | Map| HashMap example  
 
 
Java|Util|Hashmap -- HashMap implements Map Interface-Java Collections

Java Collections -HashMap implements Map Interface


Example for Java HashMap.

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;



public class HashMapDemo {

public HashMap buildHashTable(){
HashMap hashMap = new HashMap();
hashMap.put( "Name" , "Venu gopal Darur" );
hashMap.put( "RollNo" , "639" );
hashMap.put( "UNIVERSITY " , "Some University" );
hashMap.put( "City" , "Hyderabad" );
hashMap.put( "State" , "Andhra Pradesh" );
hashMap.put( "Country" , "India" );
hashMap.put( null , "India" );
return hashMap;
}

public void prinHashTable( HashMap hashMap ){
Iterator iterator = hashMap.keySet().iterator();// Iterate on keys
//We can also Iterate on values how? i.e. your homework
while ( iterator.hasNext() ){
String key = ( String ) iterator.next();
String value = ( String ) hashMap.get( key );
System.out.println( key + "\t\t: \t" + value );

}
}

public static void main( String [] args ){
HashMapDemo hashMapDemo = new HashMapDemo( );
HashMap hashMap =hashMapDemo.buildHashTable();
hashMapDemo.prinHashTable( hashMap );
}

}
 
---------------------------------------------------------------------------------------------------------------------------------------
Output :

RollNo : 639
City : Hyderabad
Name : Venugopal Darur
College : Some University
Country : India
State : Andhra Pradesh
null : India

// HashMap can store one Null Value, use this example to show that HashMap can use one null value as key.
// Hashtable cannot store one null value as key

Please send comments to vgdarur.javafive@blogger.com

 
     
 
 
 
http://www.iforerunner.com/