0% found this document useful (0 votes)
37 views

Retrieve Values From HASHMAP

There are four main ways to iterate through a map in Java: 1) Using a for-each loop to iterate over entries, 2) Using a for-each loop to iterate over just keys or values, 3) Using an iterator to loop over entries, and 4) Using a for-each loop to iterate over keys and look up the corresponding values. As of Java 8, the forEach method can also be used.

Uploaded by

mbhargavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
37 views

Retrieve Values From HASHMAP

There are four main ways to iterate through a map in Java: 1) Using a for-each loop to iterate over entries, 2) Using a for-each loop to iterate over just keys or values, 3) Using an iterator to loop over entries, and 4) Using a for-each loop to iterate over keys and look up the corresponding values. As of Java 8, the forEach method can also be used.

Uploaded by

mbhargavi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 3

HASHMAP:

Different Ways to Iterate Through a Map in Java


Updated: December 2, 2018 - Amir Ghahrai

Looping over a Map in Java. In this post, we look at four different ways we can iterate
through a map in Java. As of Java 8, we can use the forEach method as well as the
iterator class to loop over a map.

Method #1: Iterating over entries using For-Each loop

Map<Integer, Integer> map = new HashMap<Integer, Integer>();


for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " +
entry.getValue());
}

Method #2: Iterating over keys or values using For-Each loop

Map<Integer, Integer> map = new HashMap<Integer, Integer>();

//iterating over keys only


for (Integer key : map.keySet()) {
System.out.println("Key = " + key);
}

//iterating over values only


for (Integer value : map.values()) {
System.out.println("Value = " + value);
}
Method #3: Iterating using Iterator.
Using Generics:

Map<Integer, Integer> map = new HashMap<Integer, Integer>();


Iterator<Map.Entry<Integer, Integer>> entries =
map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry<Integer, Integer> entry = entries.next();
System.out.println("Key = " + entry.getKey() + ", Value = " +
entry.getValue());
}

Without Generics:

Map map = new HashMap();


Iterator entries = map.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
Integer key = (Integer)entry.getKey();
Integer value = (Integer)entry.getValue();
System.out.println("Key = " + key + ", Value = " + value);
}

Method #4: Iterating over keys and searching for values

Map<Integer, Integer> map = new HashMap<Integer, Integer>();


for (Integer key : map.keySet()) {
Integer value = map.get(key);
System.out.println("Key = " + key + ", Value = " + value);
}
Using Java 8 ForEach

Map<String, Integer> items = new HashMap<>();


items.put("key 1", 1);
items.put("key 2", 2);
items.put("key 3", 3);

items.forEach((k,v)->System.out.println("Item : " + k + "


Count :

You might also like