In next section of this Java tutorial we will see what all possible ways, to iterate map
For Example
HashMap<String, String> creditCardMap = new HashMap<String, String>();creditCardMap .put<"silver", "citibank");creditCardMap .put<"gold", "Wells Fargo");
Here will see, what all possible ways to itereate loop
- Iterate Map using forEach - (Java 5)
- Iterating Map KeySet Iterator
- Iterate HashMap in using EntrySet and forEach
- Iterating HashMap using EntrySet and Java iterator
Iterate Map using forEach - (Java 5):
Here we will use new foreach loop introduced in JDK5 for iterating over any map in java and using KeySet of map for getting keys. this will iterate through all values of Map and display key and value together.
HashMap<String, String> creditCardMap = new HashMap<String, String>();creditCardMap.put("silver", "citibank");creditCardMap.put("gold", "Wells Fargo");for (String key : creditCardMap.keySet()) { System.out.println("------------------------------------------------"); System.out.println("Iterating or looping map using java5 foreach loop"); System.out.println("key: " + key + " value: " + creditCardMap.get(key));}Output:
------------------------------------------------
Iterating or looping map using java5 foreach looop
key: silver value: citibank
------------------------------------------------
Iterating or looping map using java5 foreach looop
key: gold: Wells Fargo
Iterating Map KeySet Iterator:
In this Example of looping hashmap in Java we have used Java Iterator instead of for loop, rest are similar to earlier example of looping:
Set<String> keySet = creditCardMap.keySet();Iterator<String> keySetIterator = keySet.iterator();while (keySetIterator.hasNext()) { System.out.println("------------------------------------------------"); System.out.println("Iterating Map in Java using KeySet Iterator"); String key = keySetIterator.next(); System.out.println("key: " + key + " value: " + creditCardMap.get(key));}Output:
------------------------------------------------
Iterating or looping map using java5 foreach looop
key: silver value: citibank
------------------------------------------------
Iterating or looping map using java5 foreach looop
key: gold: Wells Fargo
Iterate HashMap in using EntrySet and forEach
In this Example of traversing Map in Java, we have used EntrySet instead of KeySet. EntrySet is a collection of all Map Entries and contains both Key and Value.
Set<Map.Entry<String, String>> entrySet = creditCardMap.entrySet();for (Entry entry : entrySet) { System.out.println("------------------------------------------------"); System.out.println("looping HashMap in Java using EntrySet and java5 for loop"); System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());}output:
------------------------------------------------
Iterating or looping map using java5 foreach looop
key: silver value: citibank
------------------------------------------------
Iterating or looping map using java5 foreach looop
key: gold: Wells Fargo
Iterating HashMap using EntrySet and Java iterator
This is the fourth and last example of looping Map and here we have used Combination of Iterator and EntrySet to display all keys and 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 in Java using EntrySet and Java iterator"); Entry entry = entrySetIterator.next(); System.out.println("key: " + entry.getKey() + " value: " + entry.getValue());}ourpur:
===========
Iterating or looping map using java5 foreach looop
key: silver value: citibank
------------------------------------------------
Iterating or looping map using java5 foreach looop
key: gold: Wells Fargo

0 comments :
Post a Comment