4 Illustration 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 around of those. But to my surprise, ane of my friends was asked inwards his interview (he has to a greater extent than than vi years of sense inwards Java programming) to write code for getting values from hashmap or TreeMap inwards Java amongst at to the lowest degree 4 ways. Just similar me he likewise surprised on this enquiry just written it. I don't know why precisely someone asks this form of java interview question to a relatively senior coffee programmer. Though my closest estimate is to verify that whether he is nevertheless hands on amongst 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, thence think this because yous may likewise inquire this enquiry J.
How to traverse or loop Map, HashMap or TreeMap inwards Java
In adjacent department of this Java tutorial, nosotros volition come across four unlike ways of looping or iterating over Map inwards Java in addition to volition display each cardinal in addition to value from HashMap. We volition role 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 role novel foreach loop introduced inwards JDK5 for iterating over whatsoever 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 cardinal 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 cardinal : loans.keySet()) {
System.out.println("------------------------------------------------");
System.out.println("Iterating or looping map using java5 foreach loop");
System.out.println("key: " + cardinal + " 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 inwards Java using KeySet Iterator
In this Example of looping hashmap inwards Java nosotros accept used Java Iterator instead of for loop, residual 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 inwards Java using KeySet Iterator");
String cardinal = keySetIterator.next();
System.out.println("key: " + cardinal + " value: " + loans.get(key));
}
Output:
------------------------------------------------
Iterating Map inwards Java using KeySet Iterator
key: habitation 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: habitation 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 instance 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.
Further Learning
Data Structures in addition to Algorithms: Deep Dive Using Java
How to debug Java programme inwards Eclipse
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: habitation 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 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 yous are familiar amongst whatsoever other ways of iterating in addition to getting each cardinal value from Map inwards Java.
Further Learning
Data Structures in addition to Algorithms: Deep Dive Using Java
How to debug Java programme inwards Eclipse
Top xxx Eclipse Keyboard Shortcuts for Java Programmer
Thanks for readin this article thence far, if yous similar this tutorial in addition to thence delight part amongst your friends in addition to colleagues.
Thanks for readin this article thence far, if yous similar this tutorial in addition to thence delight part amongst your friends in addition to colleagues.
Belum ada Komentar untuk "4 Illustration To Iterate Over Hashmap, Hashtable Or Whatever Map Inwards Java"
Posting Komentar