How to Create a HashMap in Java


In this example we will show the method to create a HashMap in Java.

Source Code

package com.beginner.examples;

import java.util.HashMap;

public class CreateHashMap {
  public static void main(String[] args) {
    HashMap h = new HashMap(); // create a HashMap
    h.put("A", "a");
    h.put("B", "b");
    System.out.println(h); 
  }
}

Output:

{A=a, B=b}

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

How to Create a HashMap in Java


In this example we will show you how to create a HashMap that should store String keys and Integer values in Java.

Source Code

package com.beginner.examples;

import java.util.HashMap;

public class CreateAHashMap {
  public static void main(String[] args) {

    HashMap h = new HashMap(); // Create a HashMap
    h.put("a", 1);
    h.put("b", 2);
    h.put("c", 3);
    System.out.println(h);
  }
}

Output:

{a=1, b=2, c=3}

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments