4 example to Iterate over HashMap, Hashtable or any Map in Java
2013-11-15 13:05
519 查看
There are multiple way to iterate, traverse or loop through Map, HashMap or
TreeMap in Java and we all familiar of either all of those or some of those. But to my surprise one of
my friends was asked in his interview (he has more than 6 years of experience in java programming) to write
code for getting values from hashmap or TreeMap in Java with at least 4 ways. Just like me he also surprised
on this question but written it. I don't know why exactly some one ask this kind of
java interview question to a relatively senior java programmer. Though my closest guess is to verify that whether he is still hands on with
coding in java. Anyway that gives me idea to write this Java
tutorial and here is multiple ways to traverse, iterate or loop on a Map in Java, so remember this because you may also ask this question J.
In
next section of this Java tutorial we will see four different ways of looping or iterating over Map in Java and will
display each key and value from HashMap. We will use following hashmap for our example:
HashMap<String, String> loans = new HashMap<String, String>();
loans.put<"home loan", "citibank");
loans.put<"personal loan", "Wells Fargo");
Iterating or looping map using Java5 foreach loop
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> loans = new HashMap<String, String>();
loans.put("home loan", "citibank");
loans.put("personal loan", "Wells Fargo");
for (String key : loans.keySet()) {
System.out.println("------------------------------------------------");
System.out.println("Iterating or looping map using java5 foreach loop");
System.out.println("key: " + key + " value: " + loans.get(key));
}
Output:
------------------------------------------------
Iterating or looping map using java5 foreach looop
key: home loan value: citibank
------------------------------------------------
Iterating or looping map using java5 foreach looop
key: personal loan value: Wells Fargo
Java Iterator instead of for loop, rest are similar to earlier example of looping:
Set<String> keySet = loans.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: " + loans.get(key));
}
Output:
------------------------------------------------
Iterating Map in Java using KeySet Iterator
key: home loan value: citibank
------------------------------------------------
Iterating Map in Java using KeySet Iterator
key: personal loan value: Wells Fargo
Set<Map.Entry<String, String>> entrySet = loans.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:
------------------------------------------------
looping HashMap in Java using EntrySet and java5 for loop
key: home loan value: citibank
------------------------------------------------
looping HashMap in Java using EntrySet and java5 for loop
key: personal loan value: Wells Fargo
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());
}
Output:
------------------------------------------------
Iterating HashMap in Java using EntrySet and Java iterator
key: home loan value: citibank
------------------------------------------------
Iterating HashMap in Java using EntrySet and Java iterator
key: personal loan value: Wells Fargo
That’s all on multiple ways of looping Map in Java. We have seen exactly
4 examples to iterator on Java Map in combination of KeySet and EntrySet by using for loop and Iterator. Let me know if you are familiar with any other ways of iterating and getting each key value from Map in Java.
Read more: http://javarevisited.blogspot.com/2011/12/how-to-traverse-or-loop-hashmap-in-java.html#ixzz2kghKEnTj
TreeMap in Java and we all familiar of either all of those or some of those. But to my surprise one of
my friends was asked in his interview (he has more than 6 years of experience in java programming) to write
code for getting values from hashmap or TreeMap in Java with at least 4 ways. Just like me he also surprised
on this question but written it. I don't know why exactly some one ask this kind of
java interview question to a relatively senior java programmer. Though my closest guess is to verify that whether he is still hands on with
coding in java. Anyway that gives me idea to write this Java
tutorial and here is multiple ways to traverse, iterate or loop on a Map in Java, so remember this because you may also ask this question J.
How to traverse or loop Map, HashMap or TreeMap in Java
In
next section of this Java tutorial we will see four different ways of looping or iterating over Map in Java and will
display each key and value from HashMap. We will use following hashmap for our example:
HashMap<String, String> loans = new HashMap<String, String>();
loans.put<"home loan", "citibank");
loans.put<"personal loan", "Wells Fargo");
Iterating or looping map using Java5 foreach loop
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> loans = new HashMap<String, String>();
loans.put("home loan", "citibank");
loans.put("personal loan", "Wells Fargo");
for (String key : loans.keySet()) {
System.out.println("------------------------------------------------");
System.out.println("Iterating or looping map using java5 foreach loop");
System.out.println("key: " + key + " value: " + loans.get(key));
}
Output:
------------------------------------------------
Iterating or looping map using java5 foreach looop
key: home loan value: citibank
------------------------------------------------
Iterating or looping map using java5 foreach looop
key: personal loan value: Wells Fargo
Iterating Map in Java using KeySet Iterator
In this Example of looping hashmap in Java we have usedJava Iterator instead of for loop, rest are similar to earlier example of looping:
Set<String> keySet = loans.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: " + loans.get(key));
}
Output:
------------------------------------------------
Iterating Map in Java using KeySet Iterator
key: home loan value: citibank
------------------------------------------------
Iterating Map in Java using KeySet Iterator
key: personal loan value: Wells Fargo
Looping HashMap in Java using EntrySet and Java 5 for loop
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 = loans.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:
------------------------------------------------
looping HashMap in Java using EntrySet and java5 for loop
key: home loan value: citibank
------------------------------------------------
looping HashMap in Java using EntrySet and java5 for loop
key: personal loan value: Wells Fargo
Iterating HashMap in Java 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());
}
Output:
------------------------------------------------
Iterating HashMap in Java using EntrySet and Java iterator
key: home loan value: citibank
------------------------------------------------
Iterating HashMap in Java using EntrySet and Java iterator
key: personal loan value: Wells Fargo
That’s all on multiple ways of looping Map in Java. We have seen exactly
4 examples to iterator on Java Map in combination of KeySet and EntrySet by using for loop and Iterator. Let me know if you are familiar with any other ways of iterating and getting each key value from Map in Java.
Read more: http://javarevisited.blogspot.com/2011/12/how-to-traverse-or-loop-hashmap-in-java.html#ixzz2kghKEnTj
相关文章推荐
- How to Iterate Over a Map in Java
- How to Iterate Over a Map in Java?(如何遍历Map)
- How to Iterate Over a Map in Java
- How to Iterate Over a Map in Java
- How to Iterate Over a Map in Java
- How to Iterate over Array in Java 1.5 using foreach loop Example
- How to Parse or Read XML File in Java >> XML Tutorial Example
- 005:What's the best way to iterate over the items in a HashMap?
- How to use ConcurrentHashMap in Java - Example Tutorial and Working
- Notes: How to iterate a map in Java
- Notes: How to iterate a map in Java
- Notes: How to iterate a map in Java
- Notes: How to iterate a map in Java
- Difference between ConcurrentHashMap and Collections.synchronizedMap and Hashtable in Java
- Notes: How to iterate a map in Java
- Notes: How to iterate a map in Java
- Notes: How to iterate a map in Java
- Hashtable and HashMap are two hash based collection in Java and used to store objects as key value p
- According to TLD or attribute directive in tag file, attribute items does not accep t any expressions
- How to upload a file via a HTTP multipart request in Java without using any external libraries