banner



Can You Add Elements To A Hashmap While Iterating

There are mostly five ways of iterating over a Map in Java. In this article, we will hash out all of them and also look at their advantages and disadvantages.
Kickoff of all, we cannot iterate a Map straight using iterators, because Map are not Drove. Also before going further, y'all must know a petty-bit about Map.Entry<Thou, V> interface.
Since all maps in Java implement Map interface, following techniques volition work for any map implementation (HashMap, TreeMap, LinkedHashMap, Hashtable, etc.)

Java-Foundation-Course

ane. Iterating over Map.entrySet() using For-Each loop :
Map.entrySet() method returns a collection-view(Ready<Map.Entry<K, 5>>) of the mappings independent in this map. So we can iterate over primal-value pair using getKey() and getValue() methods of Map.Entry<K, V>. This method is nigh common and should be used if you need both map keys and values in the loop. Below is the coffee plan to demonstrate information technology.

Coffee

import coffee.util.Map;

import java.util.HashMap;

grade IterationDemo

{

public static void main(Cord[] arg)

{

Map<String,String> gfg = new HashMap<String,String>();

gfg.put( "GFG" , "geeksforgeeks.org" );

gfg.put( "Do" , "practice.geeksforgeeks.org" );

gfg.put( "Lawmaking" , "code.geeksforgeeks.org" );

gfg.put( "Quiz" , "quiz.geeksforgeeks.org" );

for (Map.Entry<Cord,String> entry : gfg.entrySet())

Organization.out.println( "Central = " + entry.getKey() +

", Value = " + entry.getValue());

}

}

Output:

Key = Quiz, Value = quiz.geeksforgeeks.org Key = Do, Value = practice.geeksforgeeks.org Fundamental = GFG, Value = geeksforgeeks.org Central = Code, Value = code.geeksforgeeks.org

ii. Iterating over keys or values using keySet() and values() methods
Map.keySet() method returns a Set view of the keys contained in this map and Map.values() method returns a collection-view of the values independent in this map. Then If you need only keys or values from the map, yous can iterate over keySet or values using for-each loops. Below is the java program to demonstrate it.

Java

import java.util.Map;

import java.util.HashMap;

class IterationDemo

{

public static void main(String[] arg)

{

Map<String,String> gfg = new HashMap<String,String>();

gfg.put( "GFG" , "geeksforgeeks.org" );

gfg.put( "Practice" , "practice.geeksforgeeks.org" );

gfg.put( "Lawmaking" , "code.geeksforgeeks.org" );

gfg.put( "Quiz" , "quiz.geeksforgeeks.org" );

for (Cord name : gfg.keySet())

System.out.println( "key: " + name);

for (String url : gfg.values())

Arrangement.out.println( "value: " + url);

}

}

Output:

primal: Quiz key: Practice key: GFG key: Code value: quiz.geeksforgeeks.org value: practise.geeksforgeeks.org value: geeksforgeeks.org value: code.geeksforgeeks.org

3. Iterating using iterators over Map.Entry<K, V>
This method is somewhat similar to outset i. In first method we use for-each loop over Map.Entry<Grand, V>, simply hither we use iterators. Using iterators over Map.Entry<Chiliad, V> has information technology's own advantage,i.due east. nosotros can remove entries from the map during iteration by calling iterator.remove() method.

Coffee

import java.util.Map;

import java.util.HashMap;

import java.util.Iterator;

course IterationDemo

{

public static void main(String[] arg)

{

Map<Cord,String> gfg = new HashMap<String,Cord>();

gfg.put( "GFG" , "geeksforgeeks.org" );

gfg.put( "Practice" , "practise.geeksforgeeks.org" );

gfg.put( "Code" , "code.geeksforgeeks.org" );

gfg.put( "Quiz" , "quiz.geeksforgeeks.org" );

Iterator<Map.Entry<String, Cord>> itr = gfg.entrySet().iterator();

while (itr.hasNext())

{

Map.Entry<String, String> entry = itr.next();

System.out.println( "Key = " + entry.getKey() +

", Value = " + entry.getValue());

}

}

}

Output:

Key = Quiz, Value = quiz.geeksforgeeks.org Fundamental = Practice, Value = do.geeksforgeeks.org Central = GFG, Value = geeksforgeeks.org Primal = Code, Value = code.geeksforgeeks.org

4. Using forEach(activity) method :
In Java viii, you lot can iterate a map using Map.forEach(action) method and using lambda expression. This technique is clean and fast.

Java

import java.util.Map;

import java.util.HashMap;

class IterationDemo

{

public static void main(String[] arg)

{

Map<String,Cord> gfg = new HashMap<String,String>();

gfg.put( "GFG" , "geeksforgeeks.org" );

gfg.put( "Do" , "do.geeksforgeeks.org" );

gfg.put( "Code" , "code.geeksforgeeks.org" );

gfg.put( "Quiz" , "quiz.geeksforgeeks.org" );

gfg.forEach((k,v) -> System.out.println( "Key = "

+ k + ", Value = " + v));

}

}

Output :

Cardinal = Quiz, Value = quiz.geeksforgeeks.org Key = Practice, Value = practice.geeksforgeeks.org Key = GFG, Value = geeksforgeeks.org Key = Code, Value = code.geeksforgeeks.org

5. Iterating over keys and searching for values (inefficient)
Hither first nosotros loop over keys(using Map.keySet() method) and then search for value(using Map.get(key) method) for each key.This method is not used in practice equally information technology is pretty slow and inefficient as getting values by a key might be fourth dimension-consuming.

Java

import coffee.util.Map;

import java.util.HashMap;

class IterationDemo

{

public static void chief(Cord[] arg)

{

Map<Cord,String> gfg = new HashMap<String,Cord>();

gfg.put( "GFG" , "geeksforgeeks.org" );

gfg.put( "Practice" , "do.geeksforgeeks.org" );

gfg.put( "Lawmaking" , "code.geeksforgeeks.org" );

gfg.put( "Quiz" , "quiz.geeksforgeeks.org" );

for (String name : gfg.keySet())

{

String url = gfg.get(name);

Organisation.out.println( "Fundamental = " + name + ", Value = " + url);

}

}

}

Output:

Key = Quiz, Value = quiz.geeksforgeeks.org Key = Exercise, Value = practice.geeksforgeeks.org Key = GFG, Value = geeksforgeeks.org Key = Lawmaking, Value = code.geeksforgeeks.org

References : Stackoverflow
This commodity is contributed by Gaurav Miglani and Abhishek Verma. If y'all like GeeksforGeeks and would similar to contribute, you tin also write an commodity using write.geeksforgeeks.org or mail your article to review-squad@geeksforgeeks.org. Meet your article appearing on the GeeksforGeeks master folio and help other Geeks.
Please write comments if you observe anything incorrect, or you want to share more information nigh the topic discussed above.


Can You Add Elements To A Hashmap While Iterating,

Source: https://www.geeksforgeeks.org/iterate-map-java/

Posted by: johnstonyoulle.blogspot.com

0 Response to "Can You Add Elements To A Hashmap While Iterating"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel