Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
There are multiple ways to iterate or loop a Map in Java.
Using foreach in Java 8
If you using Java 8 this is the easiest way to loop the Map.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// {
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// }
Map<Integer, String> customers = new HashMap<>();
customers.put(1, "Jhon");
customers.put(2, "Smith");
customers.put(3, "Sally");
System.out.println("Using foreach in Java 8");
customers.forEach((id, name) -> {
System.out.println("Key : " + id + " value : " + name);
});
//{
}
}
//}
Press desired key combination and then press ENTER.
Using stream() in Java 8
This is also using in Java 8. If you want to filter some data while looping this is one of best way to do that.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// {
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// }
Map<Integer, String> customers = new HashMap<>();
customers.put(1, "Jhon");
customers.put(2, "Smith");
customers.put(3, "Sally");
System.out.println("");
customers.entrySet().stream().forEach(e ->
System.out.println("Key : " + e.getKey() + " value : " + e.getValue())
);
//{
}
}
//}
Press desired key combination and then press ENTER.
Using entrySet()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// {
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// }
Map<Integer, String> customers = new HashMap<>();
customers.put(1, "Jhon");
customers.put(2, "Smith");
customers.put(3, "Sally");
for (Map.Entry<Integer, String> entry : customers.entrySet()) {
System.out.println("Key : " + entry.getKey() + " value : " + entry.getValue());
}
//{
}
}
//}
Press desired key combination and then press ENTER.
Using keySet()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// {
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// }
Map<Integer, String> customers = new HashMap<>();
customers.put(1, "Jhon");
customers.put(2, "Smith");
customers.put(3, "Sally");
for (Integer key : customers.keySet()) {
System.out.println("Key : " + key + " value : " + customers.get(key));
}
//{
}
}
//}
Press desired key combination and then press ENTER.
Using iterator through map
This way is more suitable for when you need to remove some data while iterating.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// {
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// }
Map<Integer, String> customers = new HashMap<>();
customers.put(1, "Jhon");
customers.put(2, "Smith");
customers.put(3, "Sally");
Iterator<Map.Entry<Integer, String>> iterator = customers.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry entry = iterator.next();
System.out.println("Key : " + entry.getKey() + " value : " + entry.getValue());
}
//{
}
}
//}
Press desired key combination and then press ENTER.
Using iterator through the KeySet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
// {
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
public class Main {
public static void main(String[] args) {
// }
Map<Integer, String> customers = new HashMap<>();
customers.put(1, "Jhon");
customers.put(2, "Smith");
customers.put(3, "Sally");
Iterator<Integer> iterator = customers.keySet().iterator();
while (iterator.hasNext()) {
Integer key = iterator.next();
System.out.println("Key : " + key + " value : " + customers.get(key));
}
//{
}
}
//}
Press desired key combination and then press ENTER.
Original post :- http://mydevgeek.com/6-ways-iterate-loop-map-java/
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content