How to Access an Item in a HashMap in Java


In this example, we will learn the method to access an item in a HashMap in Java.

Source Code

package com.beginner.examples;

import java.util.HashMap;

public class AccessHashMap {
  public static void main(String[] args) {
    HashMap h = new HashMap(); // HashMap
    h.put(1, "a");
    h.put(2, "b");
    System.out.println(h.get(1)); // access an item in a HashMap
  }
}

Output:

a

References

Imported packages in Java documentation:

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments