
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Print Elements of a HashMap in Java
A HashMap is a subclass of AbstractMap class and it is used to store key & value pairs. Each key is mapped to a single value in the map and the keys are unique.
It means we can insert a key only once in a map and duplicate keys are not allowed, but the value can be mapped to multiple keys. We can add the elements using the put() method of HashMap class and iterate the elements using the Iterator interface.
Syntax
public V put(K key, V value)
Example
import java.util.*; import java.util.Map.*; public class HashMapTest { public static void main(String[] args) { HashMap map = new HashMap(); // adding the elements to hashmap using put() method map.put("1", "Adithya"); map.put("2", "Jai"); map.put("3", "Chaitanya"); map.put("4", "Krishna"); map.put("5", "Ramesh"); if(!map.isEmpty()) { Iterator it = map.entrySet().iterator(); while(it.hasNext()) { Map.Entry obj = (Entry)it.next(); System.out.println(obj.getValue()); } } } }
Output
Adithya Jai Chaitanya Krishna Ramesh
Advertisements