Download java util HashMap Class in Formats
QR Code for this Page
java util HashMap Class

java util HashMap Class

java.util.HashMap Class

HashMap Class in Java

The HashMap is the most commonly used implementation of the Map interface. It provides a basic key−value map where the elements are unordered. If you need to maintain map keys in an ordered fashion, that's where the TreeMap comes in handy.

The default initial capacity of the internal data structure is 16 and the default load factor is 0.75. Unlike a Hashtable, both the key and the value for a HashMap can be null. If the key happens to already be in the map, the old value is replaced and returned. Otherwise, null is returned. The map uses the key's hash code to determine where to store the key−value pair internally.

Similar to the other collection classes, the toString() returned value will be a comma−delimited list of the collection elements within braces ({}). For the HashMap, each key−value element is displayed separated by an equal sign. The listed order does not reflect the order in which the elements are added to the HashMap. Instead, the order reflects the range conversion of the hash codes generated from the keys. Removing all elements from a map does not return the space used by the internal data structure. The capacity of the structure remains the same. Only the entries of the structure are nulled out.

java.util.HashMap class Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*  Java HashMap class Example
    Save with file name HashMapExample.java */
  
import java.util.HashMap;
import java.util.Enumeration;
  
public class HashMapExample
{
  public static void main(String args[])
  {
    //java.util.HashMap DECLARATION
    HashMap <String,Integer> h;
    //java.util.HashMap OBJECT CREATION
    //USING DEFAULT CONSTRUCTOR
    h = new HashMap <String,Integer>();
    //ADD KEY AND VALUE
    h.put("ONE", new Integer(1));
    h.put("TWO", new Integer(2));
    h.put("THREE", new Integer(3));
    //ALLOW null KEY AND VALUE
    h.put(null,null);
    //HashMap OUTPUT
    System.out.println(h);
  }
}

java.util.HashMap class Example 2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*  Java HashMap class Example 2
    Save with file name HashMapExample2.java  */
  
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Collection;
import java.util.Iterator;
  
public class HashMapExample2
{
  public static void main(String args[])
  {
    //java.util.HashMap DECLARATION
    HashMap <String,Integer> h;
    //java.util.HashMap OBJECT CREATION
    //USING DEFAULT CONSTRUCTOR
    h = new HashMap <String,Integer>();
    //ADD KEY AND VALUE
    h.put("ONE", new Integer(1));
    h.put("TWO", new Integer(2));
    h.put("THREE", new Integer(3));
    //ALLOW null KEY AND VALUE
    h.put(null,null);
    //HashMap KEYS OUTPUT
    Set s = h.keySet();
    Iterator itr = s.iterator();
    int i=1;
    while(itr.hasNext())
    {
      System.out.println("Key " + i++ + " : " + itr.next());
    }
    //HashMap VALUES OUTPUT
    Collection c = h.values();
    Iterator values = c.iterator();
    int j=1;
    while(values.hasNext())
    {
      System.out.println("Value " + j++ + " : " + values.next());
    }
  }
}

java.util.HashMap class Example 3

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
/*  Java HashMap class Example 3
    Save with file name HashMapExample3.java  */
  
import java.util.HashMap;
import java.util.Map;
import java.util.Iterator;
import java.util.Set;
import java.util.Collection;
import java.util.Iterator;
  
public class HashMapExample3
{
  public static void main(String args[])
  {
    //java.util.HashMap DECLARATION
    HashMap <String,Integer> h;
    //java.util.HashMap OBJECT CREATION
    //USING DEFAULT CONSTRUCTOR
    h = new HashMap <String,Integer>();
    //ADD KEY AND VALUE
    h.put("ONE", new Integer(1));
    h.put("TWO", new Integer(2));
    h.put("THREE", new Integer(3));
    //ALLOW null KEY AND VALUE
    h.put(null,null);
    //HashMap KEYS OUTPUT
    Set s = h.entrySet();
    Iterator itr = s.iterator();
    int i=1;
    while(itr.hasNext())
    {
      //Map.Entry IS INNER INTERFACE OF Map INTERFACE
      Map.Entry entry = (Map.Entry) itr.next();
      System.out.println(entry.getKey()+" "+entry.getValue());
    }
  }
}

java.util.HashMap class Example 4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*  Java HashMap class Example 4
    Save with file name HashMapExample4.java  */
  
import java.util.HashMap;
import java.util.Enumeration;
  
public class HashMapExample4
{
  public static void main(String args[])
  {
    //java.util.HashMap DECLARATION
    HashMap <String,Integer> h;
    //java.util.HashMap OBJECT CREATION
    //USING DEFAULT CONSTRUCTOR
    h = new HashMap <String,Integer>();
    //ADD AN ELEMENTS
    h.put("ONE", new Integer(1));
    h.put("TWO", new Integer(2));
    h.put("THREE", new Integer(3));
    System.out.println("isEmpty : " + h.isEmpty());
    //RETURNS THE NUMBER OF KEYS IN THIS HashMap
    System.out.println("noof Keys : " + h.size());
    System.out.println("Key ONE value : " + h.get("ONE"));
    System.out.println("Contains key THREE : " + h.containsKey("THREE"));
    System.out.println("Contains value 2 : " + h.containsValue(new Integer(2)));
  }
}

The following example shows how to save HashMap into file.

How to save HashMap into file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/*  How to save HashMap into file Example
    Save with file name HashMapExample5.java  */
  
import java.util.HashMap;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
  
public class HashMapExample5
{
  public static void main(String args[])
  {
    try
    {
      //java.util.HashMap DECLARATION
      HashMap <String,Integer> h;
      //java.util.HashMap OBJECT CREATION
      //USING DEFAULT CONSTRUCTOR
      h = new HashMap <String,Integer>();
      //ADD AN ELEMENTS
      h.put("ONE", new Integer(1));
      h.put("TWO", new Integer(2));
      h.put("THREE", new Integer(3));
      //FileOutputStream CREATION
      FileOutputStream fos = new FileOutputStream("hashmap.set");
      //ObjectOutputStream CREATION
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      //WRITE Set OBJECT TO ObjectOutputStream
      oos.writeObject(h);
      //CLOSE THE ObjectOutputStream
      oos.close();
      System.out.println("HashMap Saved into File Sucessfully");
    }
    catch(Exception e)
    {
      System.out.println("Error Occurred : " + e.getMessage());
    }
  }
}

The following example shows how to retrieve HashMap from file.

How to retrieve HashMap from file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*  How to retrieve HashMap from file Example
    Save with file name HashMapExample6.java  */
  
import java.util.HashMap;
import java.io.FileInputStream;
import java.io.ObjectInputStream;
  
public class HashMapExample6
{
  public static void main(String args[])
  {
    try
    {
      //java.util.HashMap DECLARATION
      HashMap <String,Integer> h;
      //FileInputStream CREATION
      FileInputStream fis = new FileInputStream("hashmap.set");
      //ObjectInputStream CREATION
      ObjectInputStream ois = new ObjectInputStream(fis);
      //READ HashMap OBJECT FROM ObjectInputStream
      h = (HashMap) ois.readObject();
      ois.close();
      System.out.println(h);
    }
    catch(Exception e)
    {
      System.out.println("Error Occurred : " + e.getMessage());
    }
  }
}
Download java util HashMap Class in Formats