Tuesday, 31 July 2012

Get Synchronized Map from Java HashMap example


This java example shows how to get a synchronized Map from Java HashMap using synchronizedMap method of Collections class.


import java.util.HashMap;
import java.util.Collections;
import java.util.Map;

public class GetSynchronizedMapFromHashMapExample {

 public static void main(String[] args) {

  // create HashMap object

  HashMap hashMap = new HashMap();

  /**
   * Java HashMap is NOT synchronized. To get synchronized Map from
   * HashMap use static void synchronizedMap(Map map) method of
   * Collections class.
   **/

  Map map = Collections.synchronizedMap(hashMap);

  /**
   * Use this map object to prevent any unsynchronized access to original
   * HashMap object.
   **/

 }
}

Remove value from Java HashMap example


This Java Example shows how to remove a key value pair from HashMap object using remove method.


import java.util.HashMap;

public class RemoveValueFromHashMapExample {

 public static void main(String[] args) {

  // create HashMap object
  HashMap hMap = new HashMap();

  // add key value pairs to HashMap
  hMap.put("1", "One");
  hMap.put("2", "Two");
  hMap.put("3", "Three");

  /**
   * To remove a key value pair from HashMap use Object remove(Object key)
   * method of HashMap class. It returns either the value mapped with the
   * key or null if no value was mapped.
   **/

  Object obj = hMap.remove("2");
  System.out.println(obj + " Removed from HashMap");

 }
}


The output is:


Two Removed from HashMap

Remove all values from Java HashMap example


 This Java Example shows how to remove all values from HashMap object or empty HashMap or clear HashMap using clear method.


import java.util.HashMap;

public class EmptyHashMapExample {

 public static void main(String[] args) {

  // create HashMap object
  HashMap hMap = new HashMap();

  // add key value pairs to HashMap
  hMap.put("1", "One");
  hMap.put("2", "Two");
  hMap.put("3", "Three");

  /**
   * To remove all values or clear HashMap use void clear method() of
   * HashMap class. Clear method removes all key value pairs contained in
   * HashMap.
   **/

  hMap.clear();

  System.out.println("Total key value pairs in HashMap are : "
    + hMap.size());
 }
}



The output is:


Total key value pairs in HashMap are : 0

Get Size of Java HashMap Example


This Java Example shows how to get the size or nubmer of key value pairs stored in HashMap using size method.


import java.util.HashMap;

public class GetSizeOfHashMapExample {

 public static void main(String[] args) {

  // create HashMap object
  HashMap hMap = new HashMap();

  /**
   * To get the size of HashMap use int size() method of HashMap class. It
   * returns the number of key value pairs stored in HashMap object.
   **/
  System.out.println("Size of HashMap : " + hMap.size());

  // add key value pairs to HashMap using put method
  hMap.put("1", "One");
  hMap.put("2", "Two");
  hMap.put("3", "Three");

  System.out.println("Size of HashMap after addition : " + hMap.size());

  // remove one element from HashMap using remove method
  Object obj = hMap.remove("2");

  System.out.println("Size of HashMap after removal : " + hMap.size());
 }
}


The output is:



Size of HashMap : 0
Size of HashMap after addition : 3
Size of HashMap after removal : 2

Check if a particular value exists in Java HashMap example


This Java Example shows how to check if HashMap object contains a particular value using containsValue method of HashMap class.


import java.util.HashMap;

public class CheckParticularValueExistHashMapExample {

 public static void main(String[] args) {

  // create object of HashMap

  HashMap phoneBook = new HashMap();

  // add key value pairs to HashMap

  phoneBook.put("John", "245745");
  phoneBook.put("Joy", "245786");
  phoneBook.put("Roy", "233783");

  /**
   * To check whether a particular value exists in HashMap use boolean
   * containsValue(Object key) method of HashMap class. It returns true if
   * the value is mapped to one or more keys in the HashMap otherwise
   * false.
   **/

  boolean blnExists = phoneBook.containsValue("245745");

  System.out.println("Phone number '245745' exists in HashMap ? : "
    + blnExists);

 }
}



The output is:


Phone number '245745' exists in HashMap ? : true