Monday 20 September 2010

Hashmap in java

public class HashMapExample{
public static void main(String args[]){
// constructs a new empty HashMap with default initial capacity
HashMap hashMap = new HashMap();
/*
To specify initial capacity, use following constructor.
HashMap HashMap = new HashMap(100);
To create HashMap from map use following constructor
HashMap HashMap = new HashMap(Map myMap);
*/
hashMap.put( "One", new Integer(1) ); // adding value into HashMap
hashMap.put( "Two", new Integer(2) );
hashMap.put( "Three", new Integer(3) );
/*
IMPORTANT : We CAN NOT add primitives to the HashMap. We have to wrap it into one of the wrapper before adding.
*/
/*
To copy all key - value pairs from any Map to HashMap use putAll method.
Signature of putAll method is,
void putAll(Map m)
*/
//get number of keys present in the HashMap
System.out.println("HashMap contains " + hashMap.size() + " key value pair.");
/*
To check whether HashMap is empty or not, use isEmpty() method.
isEmpty() returns true is HashMap is empty, otherwise false.
/*
Finding particular value from the HashMap :
HashMap's containsValue method returns boolean depending upon the presense of the value in given HashMap
Signature of the containsValue method is,
boolean containsValue(Object value)
*/
if( hashMap.containsValue( new Integer(1) ) ){
System.out.println("HashMap contains 1 as value");
}else{
System.out.println("HashMap does not contain 1 as value");
}
/*
Finding particular Key from the HashMap :
HashMap's containsKey method returns boolean depending upon the presense of the key in given HashMap
Signature of the method is,
boolean containsKey(Object key)
*/
if( hashMap.containsKey("One") ){
System.out.println("HashMap contains One as key");
}else{
System.out.println("HashMap does not contain One as value");
}
/*
Use get method of HashMap to get value mapped to particular key.
Signature of the get method is,
Object get(Object key)
*/
Integer one = (Integer) hashMap.get("One");
System.out.println("Value mapped with key \"One\" is " + one);
/*
IMPORTANT:  get method returns Object, so we need to downcast it.
*/
/*
To get all keys stored in HashMap use keySet method.
Signature of the keysSet method is,
Set keySet()
*/
System.out.println("Retriving all keys from the HashMap");
Iterator iterator = hashMap.keySet().iterator();
while( iterator. hasNext() ){
System.out.println( iterator.next() );
}
/*
To get all values stored in HashMap use entrySet() method.
Signature of the entrySet() method is,
Set entrySet()
*/
System.out.println("Retriving all values from the HashMap");
iterator = hashMap.entrySet().iterator();
while( iterator. hasNext() ){
System.out.println( iterator.next() );
}
/*
To remove particular key - value pair from the HashMap use remove method.
Signature of remove methid is,
Object remove(Object key)
This method returns value that had mapped to the given key, otherwise null if mapping not found.
*/
System.out.println( hashMap.remove("One") + " is removed from the HashMap.");
}
}

No comments:

Post a Comment