4 Example To Iterate Over Hashmap, Hashtable Or Whatsoever Map Inward Java

Advertisement

Masukkan script iklan 970x90px

4 Example To Iterate Over Hashmap, Hashtable Or Whatsoever Map Inward Java

Selasa, 21 April 2020

There are multiple ways to iterate, traverse or loop through Map, HashMap or TreeMap in Java in addition to nosotros all familiar of either all of those or around of those. But to my surprise, ane of my friends was asked inwards his interview (he has to a greater extent than than six years of sense inwards Java programming) to write code for getting values from hashmap or TreeMap inwards Java alongside at to the lowest degree 4 ways. Just similar me he besides surprised on this query just written it. I don't know why just someone asks this sort of java interview question to a relatively senior coffee programmer. Though my closest estimate is to verify that whether he is nevertheless hands on alongside coding inwards java. Anyway, that gives me an thought to write this Java tutorial in addition to hither are multiple ways to traverse, iterate or loop on a Map inwards Java, thus recollect this because yous may besides inquire this query J.


How to traverse or loop Map, HashMap or TreeMap inwards Java

 in addition to nosotros all familiar of either all of those or around of those 4 event to Iterate over HashMap, Hashtable or whatever Map inwards JavaIn adjacent department of this Java tutorial, nosotros volition encounter four unlike ways of looping or iterating over Map inwards Java in addition to volition display each fundamental in addition to value from HashMap. We volition purpose next hashmap for our example:

HashMap<String, String> loans = novel HashMap<String, String>();
loans.put<"home loan", "Citibank");
loans.put<"personal loan", "Wells Fargo");



Iterating or looping map using Java five foreach loop

Here nosotros volition purpose novel foreach loop introduced inwards JDK5 for iterating over whatever map inwards coffee in addition to using KeySet of the map for getting keys. this volition iterate through all values of Map in addition to display fundamental in addition to value together.

HashMap<String, String> loans = novel HashMap<String, String>();
loans.put("home loan", "citibank");
loans.put("personal loan", "Wells Fargo");

for (String fundamental : loans.keySet()) {
   System.out.println("------------------------------------------------");
   System.out.println("Iterating or looping map using java5 foreach loop");
   System.out.println("key: " + fundamental + " value: " + loans.get(key));
}

Output:
------------------------------------------------
Iterating or looping map using java5 foreach loop
key: abode loan value: Citibank
------------------------------------------------
Iterating or looping map using java5 foreach loop
key: personal loan value: Wells Fargo




Iterating Map inwards Java using KeySet Iterator

In this Example of looping hashmap inwards Java nosotros accept used Java Iterator instead of for loop, residuum are similar to before event of looping:

Set<String> keySet = loans.keySet();
Iterator<String> keySetIterator = keySet.iterator();
while (keySetIterator.hasNext()) {
   System.out.println("------------------------------------------------");
   System.out.println("Iterating Map inwards Java using KeySet Iterator");
   String fundamental = keySetIterator.next();
   System.out.println("key: " + fundamental + " value: " + loans.get(key));
}

Output:
------------------------------------------------
Iterating Map inwards Java using KeySet Iterator
key: abode loan value: Citibank
------------------------------------------------
Iterating Map inwards Java using KeySet Iterator
key: personal loan value: Wells Fargo




Looping HashMap inwards Java using EntrySet in addition to Java five for loop

In this Example of traversing Map inwards Java, nosotros accept used EntrySet instead of KeySet. EntrySet is a collection of all Map Entries in addition to contains both Key in addition to Value.

Set<Map.Entry<String, String>> entrySet = loans.entrySet();
for (Entry entry : entrySet) {
   System.out.println("------------------------------------------------");
   System.out.println("looping HashMap inwards Java using EntrySet in addition to java5 for loop");
   System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
}

Output:
------------------------------------------------
looping HashMap inwards Java using EntrySet in addition to java5 for loop
key: abode loan value: Citibank
------------------------------------------------
looping HashMap inwards Java using EntrySet in addition to java5 for loop
key: personal loan value: Wells Fargo




Iterating HashMap inwards Java using EntrySet in addition to Java iterator

This is the 4th in addition to final event of looping Map in addition to hither nosotros accept used Combination of Iterator in addition to EntrySet to display all keys in addition to values of a Java Map.

Set<Map.Entry<String, String>> entrySet1 = loans.entrySet();
Iterator<Entry<String, String>> entrySetIterator = entrySet1.iterator();
while (entrySetIterator.hasNext()) {
   System.out.println("------------------------------------------------");
   System.out.println("Iterating HashMap inwards Java using EntrySet in addition to Java iterator");
   Entry entry = entrySetIterator.next();
   System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
}

Output:
------------------------------------------------
Iterating HashMap inwards Java using EntrySet in addition to Java iterator
key: abode loan value: Citibank
------------------------------------------------
Iterating HashMap inwards Java using EntrySet in addition to Java iterator
key: personal loan value: Wells Fargo


That’s all on multiple ways of looping Map inwards Java. We accept seen just 4 examples to iterator on Java Map in a combination of KeySet in addition to EntrySet past times using for loop in addition to Iterator. Let me know if yous are familiar alongside whatever other ways of iterating in addition to getting each fundamental value from Map inwards Java.

Top thirty Eclipse Keyboard Shortcuts for Java Programmer

Thanks for readin this article thus far, if yous similar this tutorial in addition to thus delight percentage alongside your friends in addition to colleagues.