Traverse through a HashMap in Java
HashMap is a part of the java collections framework. It internally uses the hashing technique.
This post contains different ways of traversing through a HashMap, which is given below:
- Using an Iterator: Iterator is an interface in java.util package which is used to iterate through a collection.
- hm.entrySet() is used to retrieve all the key-value pairs called Map.Entries and stores internally into a set.
- hm.entrySet().iterator() returns an iterator which acts as a cursor and points at the first element of the set and moves on till the end.
- hmIterator.hasNext() checks for the next element in the set and returns a boolean
- hmIterator.next() returns the next element(Map.Entry) from the set.
- mapElement.getKey() returns the key of the associated Map.Entry
- mapElement.getValue() return the value of the associated Map.Entry
Example:
- filter_none
edit
play_arrow
brightness_4
Output:// Java program to traverse through// a hashmap using iteratorimportjava.util.*;classGfG {publicstaticvoidmain(String[] args){// Consider the hashmap contains// student name and their marksHashMap<String, Integer> hm =newHashMap<String, Integer>();// Adding mappings to HashMaphm.put("GeeksforGeeks",54);hm.put("A computer portal",80);hm.put("For geeks",82);// Printing the HashMapSystem.out.println("Created hashmap is"+ hm);// Getting an iteratorIterator hmIterator = hm.entrySet().iterator();// Iterate through the hashmap// and add some bonus marks for every studentSystem.out.println("HashMap after adding bonus marks:");while(hmIterator.hasNext()) {Map.Entry mapElement = (Map.Entry)hmIterator.next();intmarks = ((int)mapElement.getValue() +10);System.out.println(mapElement.getKey() +" : "+ marks);}}}Created hashmap is{GeeksforGeeks=54, A computer portal=80, For geeks=82} HashMap after adding bonus marks: GeeksforGeeks : 64 A computer portal : 90 For geeks : 92 - Using for-each loop:filter_none
edit
play_arrow
brightness_4
Output:// Java program for traversing// through a hashmap using for-each loopimportjava.util.*;classGfG {publicstaticvoidmain(String[] args){// Consider the hashmap containing// student name and their marksHashMap<String, Integer> hm =newHashMap<String, Integer>();// Adding mappings to HashMaphm.put("GeeksforGeeks",54);hm.put("A computer portal",80);hm.put("For geeks",82);// Printing the HashMapSystem.out.println("Created hashmap is"+ hm);// Loop through the hashmapSystem.out.println("HashMap after adding bonus marks:");// Using for-each loopfor(Map.Entry mapElement : hm.entrySet()) {String key = (String)mapElement.getKey();// Add some bonus marks// to all the students and print itintvalue = ((int)mapElement.getValue() +10);System.out.println(key +" : "+ value);}}}Created hashmap is{GeeksforGeeks=54, A computer portal=80, For geeks=82} HashMap after adding bonus marks: GeeksforGeeks : 64 A computer portal : 90 For geeks : 92 - Using forEach() method: forEach() is a method of hashmap which is introduced in java 8. It is used to iterate through the hashmap and also reduces the number of lines of code.filter_none
edit
play_arrow
brightness_4
Output:// Java program for traversing// through a hashmap using for-each loopimportjava.util.*;classGfG {publicstaticvoidmain(String[] args){// Consider the hashmap containing// student name and their marksHashMap<String, Integer> hm =newHashMap<String, Integer>();// Adding mappings to HashMaphm.put("GeeksforGeeks",54);hm.put("A computer portal",80);hm.put("For geeks",82);// Printing the HashMapSystem.out.println("Created hashmap is"+ hm);// Loop through the hashmap// and add bonus marksSystem.out.println("HashMap after adding bonus marks:");// Using Hashmap.forEach()hm.forEach((k, v) -> System.out.println(k +" : "+ (v +10)));}}Created hashmap is{GeeksforGeeks=54, A computer portal=80, For geeks=82} HashMap after adding bonus marks: GeeksforGeeks : 64 A computer portal : 90 For geeks : 92




post a comment