Displaying content of a HashMap in Java


Let us first create a HashMap and add elements −

HashMap hm = new HashMap();
hm.put("Wallet", new Integer(700));
hm.put("Belt", new Integer(600));

To display the content, just print the HashMap object −

System.out.println("Map = "+hm);

The following is an example to display content of a HashMap −

Example

 Live Demo

import java.util.*;
public class Demo {
   public static void main(String args[]) {
      // Create hash map
      HashMap hm = new HashMap();
      hm.put("Wallet", new Integer(700));
      hm.put("Belt", new Integer(600));
      System.out.println("Map = "+hm);
   }
}

Output

Map = {Belt=600, Wallet=700}

Updated on: 30-Jul-2019

263 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements