If you’re a programmer, even if you’re just starting out or just learning, there’s a technique you’re probably familiar with, hashMap. It’s a common technique used in daily development, and it’s often asked in interviews. Traversal is the most important part of the interview question. Can you really explain all five ways to traverse a HashMap? Not really. Or can you find out what the five traversal ways are, but can you write them out by hand?
Ok, no problem, review if there is, study if there is not, let’s have a look at the five traversal ways I organized
Use Iterator to traverse the HashMap EntrySet
Use Iterator to traverse the HashMap KeySet
Iterate over the HashMap using a for-each loop
Use Lambda expressions to traverse the HashMap
Use the Stream API to traverse the HashMap
1. Use Iterator to traverse the HashMap EntrySet
package com.java.tutorials.iterations;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
/ * *
* 5 best ways to traverse a HashMap in Java
* @author Ramesh Fadatare
*
* /
public class IterateHashMapExample {
public static void main(String[] args) {
// 1. Use Iterator to traverse the HashMap EntrySet
Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
coursesMap.put(1, “C”);
coursesMap.put(2, “C++”);
coursesMap.put(3, “Java”);
coursesMap.put(4, “Spring Framework”);
coursesMap.put(5, “Hibernate ORM framework”);
Iterator < Entry < Integer, String >> iterator = coursesMap.entrySet().iterator();
while (iterator.hasNext()) {
Entry < Integer, String > entry = iterator.next();
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}
}
Output:
1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework
2. Use Iterator to traverse the HashMap KeySet
package com.java.tutorials.iterations;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
/ * *
* 5 best ways to traverse a HashMap in Java
* @author Ramesh Fadatare
*
* /
public class IterateHashMapExample {
public static void main(String[] args) {
Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
coursesMap.put(1, “C”);
coursesMap.put(2, “C++”);
coursesMap.put(3, “Java”);
coursesMap.put(4, “Spring Framework”);
coursesMap.put(5, “Hibernate ORM framework”);
// 2. Use Iterator to traverse the HashMap KeySet
Iterator < Integer > iterator = coursesMap.keySet().iterator();
while (iterator.hasNext()) {
Integer key = iterator.next();
System.out.println(key);
System.out.println(coursesMap.get(key));
}
}
}
Output:
1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework
3. Iterate over the HashMap using a for-each loop
package com.java.tutorials.iterations;
import java.util.HashMap;
import java.util.Map;
/ * *
* 5 best ways to traverse a HashMap in Java
* @author Ramesh Fadatare
*
* /
public class IterateHashMapExample {
public static void main(String[] args) {
Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
coursesMap.put(1, “C”);
coursesMap.put(2, “C++”);
coursesMap.put(3, “Java”);
coursesMap.put(4, “Spring Framework”);
coursesMap.put(5, “Hibernate ORM framework”);
// 3. Use the for-each loop to iterate over the HashMap
for (Map.Entry < Integer, String > entry: coursesMap.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}
}
Output:
1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework
4. Use Lambda expressions to traverse the HashMap
package com.java.tutorials.iterations;
import java.util.HashMap;
import java.util.Map;
/ * *
* 5 best ways to traverse a HashMap in Java
* @author Ramesh Fadatare
*
* /
public class IterateHashMapExample {
public static void main(String[] args) {
Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
coursesMap.put(1, “C”);
coursesMap.put(2, “C++”);
coursesMap.put(3, “Java”);
coursesMap.put(4, “Spring Framework”);
coursesMap.put(5, “Hibernate ORM framework”);
// 4. Use Lambda expressions to traverse the HashMap
coursesMap.forEach((key, value) -> {
System.out.println(key);
System.out.println(value);
});
}
}
Output:
1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework
5. Use the Stream API to traverse the HashMap
package com.java.tutorials.iterations;
import java.util.HashMap;
import java.util.Map;
/ * *
* 5 best ways to traverse a HashMap in Java
* @author Ramesh Fadatare
*
* /
public class IterateHashMapExample {
public static void main(String[] args) {
Map < Integer, String > coursesMap = new HashMap < Integer, String > ();
coursesMap.put(1, “C”);
coursesMap.put(2, “C++”);
coursesMap.put(3, “Java”);
coursesMap.put(4, “Spring Framework”);
coursesMap.put(5, “Hibernate ORM framework”);
// 5. Use the Stream API to traverse the HashMap
coursesMap.entrySet().stream().forEach((entry) – > {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
});
}
}
Output:
1
C
2
C++
3
Java
4
Spring Framework
5
Hibernate ORM framework
Feel helpful to yourself, welcome to praise attention forwarding
Need more architecture information and video on cache breakdown, add wechat official account: Java Architect Union
Or scan the QR code to add
Xiaobian will issue articles and technical explanation videos from time to time, thank you