- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Display HashMap elements in Java
Create a HashMap −
HashMap hm = new HashMap();
Add elements to the HashMap that we will be displaying afterward −
hm.put("Maths", new Integer(98)); hm.put("Science", new Integer(90)); hm.put("English", new Integer(97)); hm.put("Physics", new Integer(91));
Now, to display the HashMap elements, use Iterator. The following is an example to display HashMap elements −
Example
import java.util.*; public class Demo { public static void main(String args[]) { // Create a hash map HashMap hm = new HashMap(); // Put elements to the map hm.put("Maths", new Integer(98)); hm.put("Science", new Integer(90)); hm.put("English", new Integer(97)); hm.put("Physics", new Integer(91)); hm.put("Chemistry", new Integer(93)); // Get a set of the entries Set set = hm.entrySet(); // Get an iterator Iterator i = set.iterator(); // Display elements while(i.hasNext()) { Map.Entry me = (Map.Entry)i.next(); System.out.print(me.getKey() + ": "); System.out.println(me.getValue()); } System.out.println(); System.out.println("Elements: "+hm); } }
Output
Maths: 98 English: 97 Chemistry: 93 Science: 90 Physics: 91 Elements: {Maths=98, English=97, Chemistry=93, Science=90, Physics=91}
- Related Articles
- Add elements to HashMap in Java
- Get the count of elements in HashMap in Java
- Java Program to Iterate through Elements of HashMap
- HashMap in Java
- Retrieve a set of Map.Entry elements from a HashMap in Java
- How to print the elements of a HashMap in Java?\n
- Clone HashMap in Java
- Initialize HashMap in Java
- Create a HashMap in Java
- Hashmap vs WeakHashMap in Java
- Remove value from HashMap in Java
- Set Date value in Java HashMap?
- Extract values from HashMap in Java
- Traverse through a HashMap in Java
- Internal Working of HashMap in Java

Advertisements