View RSS Feed

Algorithm and Data Structure

Java Hash Map

Rate this Entry
by , 02-19-2012 at 08:19 AM (807 Views)
Key value paired data structure is provided by the Map interface so that for mapping the keys to values. Keys are unique which are used for retrieval of the values, from Map.

Key features are as following:

  • Key value repair is used for storage of all values. Key is used for the retrieval.
  • When Map doesn’t contain any element, various methods throw the “NoSuchElementException”.
  • When compatibility of element is not present with map elements, ClassCastException gets thrown.
  • In Map, Null objects are not permitted. In these cases, “NullPointerException” would be thrown.

Java Code: A map is declared by this code.
HashMap<String, Object> myMap = new HashMap<String, Object>();
Map<String, Object> myMap = new HashMap<String, Object>();
  • Interface implemented is the major thing which differentiates the two mentioned approaches.
  • Map<String,Object> shall be used. This helps to provide the substitute for underlying implementation.

Java Code: Java Hash Map Example
import java.util.*;
public class MyMapDemo {
	public static void main(String[] args) {
	Map m1 = new HashMap();
	m1.put("Ankit", "8");
	m1.put("Kapil", "31");
	m1.put("Saurabh", "12");
	m1.put("Apoorva", "14");
	System.out.println("Elements of Map");

Submit "Java Hash Map" to Facebook Submit "Java Hash Map" to Digg Submit "Java Hash Map" to Submit "Java Hash Map" to StumbleUpon Submit "Java Hash Map" to Google

Tags: hash map Add / Edit Tags