
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
Traversing Contents of a Hash Map in Java
A map is a collection in Java which stores key-value pairs. The keys of this must not be null and each key should point to only one value. It is represented by the Map interface of java.util package. There are various classes which provide implementation to this interface.
The HashMap is a class which implements the Map interface. It is based on the Hash table. It allows null values and null keys.
In short, you can store key-value pairs in the HashMap object. Once you do so you can retrieve the values of the respective keys but, the values we use for keys should be unique.
Example
import java.util.HashMap; import java.util.Scanner; public class HashMapExample { public static void main(String args[]) { HashMap<String, Long> map = new HashMap<String, Long>(); System.out.println("Enter the number of records you need to store: "); Scanner sc = new Scanner(System.in); int num = sc.nextInt(); for(int i=0; i<num; i++) { System.out.println("Enter key (String): "); String key = sc.next(); System.out.println("Enter value (Long): "); long value = sc.nextLong(); map.put(key, value); } System.out.println("Values Stored . . . . . ."); System.out.println("Enter a name (key): "); String reqKey = sc.next(); System.out.println("Phone number (value): "+map.get(reqKey)); } }
Output
Enter the number of records you need to store: 3 Enter key (String): Krishna Enter value (Long): 9848022337 Enter key (String): Vishnu Enter value (Long): 9848022338 Enter key (String): Moksha Enter value (Long): 9848022339 Values Stored . . . . . . Enter a name (key): Krishna Phone number (value): 9848022337
Retrieving the contents of a HashMap
You can retrieve the contents of HashMap objects using the Iterator, using for each method and using the forEach() method.
Using the Iterator
The entrySet() of the HashMap class returns the set view of the current (HashMap) object.
Invoke the iterator() method on the obtained Set object. This method returns the Iterator object of the current HashMap.
The hasNext() method of the iterator class returns true if the iterator contains more elements, the next() method of it returns the next map entry.
The getKey() and getValue() methods of the Map.Entry returns key and values respectively.
Example
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class HashMapExample { public static void main(String args[]) { HashMap<String, Long> map = new HashMap<String, Long>(); map.put("Krishna", 9000123456L); map.put("Rama", 9000234567L); map.put("Sita", 9000345678L); map.put("Bhima", 9000456789L); map.put("Yousuf ", 9000456789L); System.out.println("Values Stored . . . . . ."); //Retrieving the values of a Hash map Iterator it = map.entrySet().iterator(); System.out.println("Contents of the hashMap are: "); while(it.hasNext()){ Map.Entry <String, Long> ele = (Map.Entry) it.next(); System.out.print(ele.getKey()+" : "); System.out.print(ele.getValue()); System.out.println(); } } }
Output
Values Stored . . . . . . Contents of the hashMap are: Yousuf : 9000456789 Krishna : 9000123456 Sita : 9000345678 Rama : 9000234567 Bhima : 9000456789
Example: Using for each loop
import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class HashMapExample { public static void main(String args[]) { HashMap<String, Long> map = new HashMap<String, Long>(); map.put("Krishna", 9000123456L); map.put("Rama", 9000234567L); map.put("Sita", 9000345678L); map.put("Bhima", 9000456789L); map.put("Yousuf ", 9000456789L); System.out.println("Values Stored . . . . . ."); //Retrieving the values of a Hash map Iterator it = map.entrySet().iterator(); System.out.println("Contents of the hashMap are: "); for(Map.Entry ele : map.entrySet()){ System.out.print(ele.getKey()+" : "); System.out.print(ele.getValue()); System.out.println(); } } }
Output
Values Stored . . . . . . Contents of the hashMap are: Yousuf : 9000456789 Krishna : 9000123456 Sita : 9000345678 Rama : 9000234567 Bhima : 9000456789
Using forEach() method
Since Java8 a method named forEach() is introduces to retrieve the contents of the HashMap. This is the easiest way to retrieve elements from a HashMap
Example
import java.util.HashMap; public class HashMapExample { public static void main(String args[]) { HashMap<String, Long> map = new HashMap<String, Long>(); map.put("Krishna", 9000123456L); map.put("Rama", 9000234567L); map.put("Sita", 9000345678L); map.put("Bhima", 9000456789L); map.put("Yousuf ", 9000456789L); System.out.println("Values Stored . . . . . ."); //Retrieving the values of a Hash map System.out.println("Contents of the hashMap are: "); map.forEach((k, v) -> System.out.println(k +": " + (v))); } }
Output
Values Stored . . . . . . Contents of the hashMap are: Yousuf: 9000456789 Krishna: 9000123456 Sita: 9000345678 Rama: 9000234567 Bhima: 9000456789