- 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
Retrieve a set of Map.Entry elements from a HashMap in Java
Create a HashMap and add elements to it −
HashMap hm = new HashMap(); hm.put("Wallet", new Integer(700)); hm.put("Belt", new Integer(600)); hm.put("Backpack", new Integer(1200));
The following is the code snippet to retrieve a set of Map.Entry elements −
Set s = hm.entrySet(); Iterator iter = s.iterator(); System.out.println("
Key\tValue"); while (iter.hasNext()) { Map.Entry e = (Map.Entry) iter.next(); System.out.println(e.getKey() + " " + e.getValue()); }
The following is an example to retrieve a set of Map.Entry elements from a HashMap −
Example
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)); hm.put("Backpack", new Integer(1200)); System.out.println("Map = "+hm); Set s = hm.entrySet(); Iterator iter = s.iterator(); System.out.println("
Key\tValue"); while (iter.hasNext()) { Map.Entry e = (Map.Entry) iter.next(); System.out.println(e.getKey() + " " + e.getValue()); } } }
Output
Map = {Backpack=1200, Belt=600, Wallet=700} KeyValue Backpack 1200 Belt 600 Wallet 700
Advertisements