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: 2019-07-30T22:30:24+05:30

390 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements