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

Integer For (Map - Entry Entry: Map - Entryset ) (System - Out.Println ("Key " + Entry - Getkey + ", Value " + Entry - Getvalue ) )

The document discusses 4 methods for iterating over entries in a Java Map: 1. Using a for-each loop to iterate over the entrySet, which allows accessing both the key and value and is the most common approach. 2. Iterating separately over just the keySet or values to access only keys or values. 3. Using an Iterator to loop through the entrySet, which provides generics support and avoids NullPointerExceptions. 4. Iterating over just the keySet and looking up each value separately, which is inefficient.

Uploaded by

venkraj_iitm
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Integer For (Map - Entry Entry: Map - Entryset ) (System - Out.Println ("Key " + Entry - Getkey + ", Value " + Entry - Getvalue ) )

The document discusses 4 methods for iterating over entries in a Java Map: 1. Using a for-each loop to iterate over the entrySet, which allows accessing both the key and value and is the most common approach. 2. Iterating separately over just the keySet or values to access only keys or values. 3. Using an Iterator to loop through the entrySet, which provides generics support and avoids NullPointerExceptions. 4. Iterating over just the keySet and looking up each value separately, which is inefficient.

Uploaded by

venkraj_iitm
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

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

This is the most common method and is preferable in most cases. Should be used if you need both map keys and values in the 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()); }
Note that For-Each loop was introduced in Java 5 so this method is working only in newer versions of the language. Also For-Each loop will throw NullPointerException if you try to iterate over a map that is null, so before iterating you should always check for null references.

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


If you need only keys or values from the map, you can iterate over keySet or values instead of entrySet.

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); }
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); }

Iterating over keys and searching for values (inefficient).


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); } for (Iterator<T> secret = x.iterator(); secret.hasNext(); /*see body*/) { final v = secret.next(); body }

You might also like