4 Event To Iterate Over Hashmap, Hashtable Or Whatever Map Inwards Java

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 to a greater extent than or less of those. But to my surprise, i of my friends was asked inward his interview (he has to a greater extent than than half dozen years of sense inward Java programming) to write code for getting values from hashmap or TreeMap inward Java alongside at to the lowest degree 4 ways. Just similar me he likewise surprised on this inquiry simply written it. I don't know why precisely 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 inward 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 inward Java, hence recollect this because y'all may likewise inquire this inquiry J.


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

 in addition to nosotros all familiar of either all of those or to a greater extent than or less of those 4 instance to Iterate over HashMap, Hashtable or whatever Map inward JavaIn side past times side department of this Java tutorial, nosotros volition encounter four dissimilar ways of looping or iterating over Map inward Java in addition to volition display each telephone substitution 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 inward JDK5 for iterating over whatever map inward coffee in addition to using KeySet of the map for getting keys. this volition iterate through all values of Map in addition to display telephone substitution 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 telephone substitution : loans.keySet()) {
   System.out.println("------------------------------------------------");
   System.out.println("Iterating or looping map using java5 foreach loop");
   System.out.println("key: " + telephone substitution + " value: " + loans.get(key));
}

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




Iterating Map inward Java using KeySet Iterator

In this Example of looping hashmap inward Java nosotros bring used Java Iterator instead of for loop, balance are similar to before instance of looping:

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

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




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

In this Example of traversing Map inward Java, nosotros bring 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 inward Java using EntrySet in addition to java5 for loop");
   System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
}

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




Iterating HashMap inward Java using EntrySet in addition to Java iterator

This is the 4th in addition to final instance of looping Map in addition to hither nosotros bring 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 inward Java using EntrySet in addition to Java iterator");
   Entry entry = entrySetIterator.next();
   System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());
}

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


That’s all on multiple ways of looping Map inward Java. We bring seen precisely 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 y'all are familiar alongside whatever other ways of iterating in addition to getting each telephone substitution value from Map inward Java.

Top xxx Eclipse Keyboard Shortcuts for Java Programmer

Thanks for readin this article hence far, if y'all similar this tutorial hence delight part alongside your friends in addition to colleagues. 

Sumber https://javarevisited.blogspot.com/

0 Response to "4 Event To Iterate Over Hashmap, Hashtable Or Whatever Map Inwards Java"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel