Displaying content of a HashMap in Java

Java 8Object Oriented ProgrammingProgramming

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}
raja
Updated on 30-Jul-2019 22:30:24

Advertisements