Com repetir qualsevol mapa a Java
En general n'hi ha cinc maneres d'iterar sobre a Mapa en Java. En aquest article, parlarem de tots ells i també veurem els seus avantatges i desavantatges.
En primer lloc, nosaltres no pot itereu un mapa directament utilitzant iteradors , perquè Map no ho són Col · lecció. A més, abans d'anar més enllà, cal saber-ne una mica Mapa.Entrada interfície.
Atès que tots els mapes en Java implementen Mapa interfície, les tècniques següents funcionaran per a qualsevol implementació de mapa ( HashMap , TreeMap , LinkedHashMap , Hashtable , etc.)
1. Iterant sobre Map.entrySet() utilitzant el bucle For-Each :
Map.entrySet() El mètode retorna una vista de col·lecció ( Conjunt
Java
// Java program to demonstrate iteration over> // Map.entrySet() entries using for-each loop> > import> java.util.Map;> import> java.util.HashMap;> > class> IterationDemo> {> > public> static> void> main(String[] arg)> > {> > Map gfg => new> HashMap();> > > // enter name/url pair> > gfg.put(> 'GFG'> ,> 'techcodeview.com'> );> > gfg.put(> 'Practice'> ,> 'practice .techcodeview.com> );> > gfg.put(> 'Code'> ,> 'code .techcodeview.com> );> > gfg.put(> 'Quiz'> ,> 'www .techcodeview.com> );> > > // using for-each loop for iteration over Map.entrySet()> > for> (Map.Entry entry : gfg.entrySet())> > System.out.println(> 'Key = '> + entry.getKey() +> > ', Value = '> + entry.getValue());> > }> }> |
Sortida:
Key = Quiz, Value = www.techcodeview.com Key = Practice, Value = practice.techcodeview.com Key = GFG, Value = techcodeview.com Key = Code, Value = code.techcodeview.com
2. Iterar sobre claus o valors utilitzant els mètodes keySet() i values().
Map.keySet() El mètode retorna una vista de conjunt de les claus contingudes en aquest mapa i Map.values() El mètode retorna una vista de col·lecció dels valors continguts en aquest mapa. Per tant, si només necessiteu claus o valors del mapa, podeu iterar sobre el conjunt de claus o els valors utilitzant bucles for-each. A continuació es mostra el programa java per demostrar-ho.
Java
// Java program to demonstrate iteration over> // Map using keySet() and values() methods> > import> java.util.Map;> import> java.util.HashMap;> > class> IterationDemo> {> > public> static> void> main(String[] arg)> > {> > Map gfg => new> HashMap();> > > // enter name/url pair> > gfg.put(> 'GFG'> ,> 'techcodeview.com'> );> > gfg.put(> 'Practice'> ,> 'practice .techcodeview.com> );> > gfg.put(> 'Code'> ,> 'code .techcodeview.com> );> > gfg.put(> 'Quiz'> ,> 'www .techcodeview.com> );> > > // using keySet() for iteration over keys> > for> (String name : gfg.keySet())> > System.out.println(> 'key: '> + name);> > > // using values() for iteration over values> > for> (String url : gfg.values())> > System.out.println(> 'value: '> + url);> > }> }> |
Sortida:
key: Quiz key: Practice key: GFG key: Code value: www.techcodeview.com value: practice.techcodeview.com value: techcodeview.com value: code.techcodeview.com
3. Iteració utilitzant iteradors Mapa.Entrada
Aquest mètode és una mica similar al primer. En el primer mètode fem servir for-each bucle sobre Map.Entry, però aquí fem servir iteradors . L'ús d'iteradors sobre Map.Entry té el seu propi avantatge, és a dir. podem eliminar entrades del mapa durant la iteració trucant iterator.remove() mètode.
Java
// Java program to demonstrate iteration over> // Map using keySet() and values() methods> > import> java.util.Map;> import> java.util.HashMap;> import> java.util.Iterator;> > class> IterationDemo> {> > public> static> void> main(String[] arg)> > {> > Map gfg => new> HashMap();> > > // enter name/url pair> > gfg.put(> 'GFG'> ,> 'techcodeview.com'> );> > gfg.put(> 'Practice'> ,> 'practice .techcodeview.com> );> > gfg.put(> 'Code'> ,> 'code .techcodeview.com> );> > gfg.put(> 'Quiz'> ,> 'www .techcodeview.com> );> > > // using iterators> > Iterator itr = gfg.entrySet().iterator();> > > while> (itr.hasNext())> > {> > Map.Entry entry = itr.next();> > System.out.println(> 'Key = '> + entry.getKey() +> > ', Value = '> + entry.getValue());> > }> > }> }> |
Sortida:
Key = Quiz, Value = www.techcodeview.com Key = Practice, Value = practice.techcodeview.com Key = GFG, Value = techcodeview.com Key = Code, Value = code.techcodeview.com
4. Utilitzant el mètode forEach (acció):
A Java 8, podeu iterar un mapa utilitzant Map.forEach (acció) mètode i ús expressió lambda . Aquesta tècnica és neta i ràpida.
Java
// Java code illustrating iteration> // over map using forEach(action) method> > import> java.util.Map;> import> java.util.HashMap;> > class> IterationDemo> {> > public> static> void> main(String[] arg)> > {> > Map gfg => new> HashMap();> > > // enter name/url pair> > gfg.put(> 'GFG'> ,> 'techcodeview.com'> );> > gfg.put(> 'Practice'> ,> 'practice .techcodeview.com> );> > gfg.put(> 'Code'> ,> 'code .techcodeview.com> );> > gfg.put(> 'Quiz'> ,> 'www .techcodeview.com> );> > > // forEach(action) method to iterate map> > gfg.forEach((k,v) ->System.out.println(> 'Key = '> > + k +> ', Value = '> + v));> > > }> }> |
Sortida:
Key = Quiz, Value = www.techcodeview.com Key = Practice, Value = practice.techcodeview.com Key = GFG, Value = techcodeview.com Key = Code, Value = code.techcodeview.com
5. Iterar sobre claus i cercar valors (ineficient)
Aquí primer fem un bucle sobre les claus (usant Map.keySet() mètode) i després cerqueu el valor (usant Map.get(clau) mètode) per a cada clau. Aquest mètode no s'utilitza a la pràctica, ja que és bastant lent i ineficient, ja que obtenir valors mitjançant una clau pot ser molt llarg.
Java
// Java program to demonstrate iteration> // over keys and searching for values> > import> java.util.Map;> import> java.util.HashMap;> > class> IterationDemo> {> > public> static> void> main(String[] arg)> > {> > Map gfg => new> HashMap();> > > // enter name/url pair> > gfg.put(> 'GFG'> ,> 'techcodeview.com'> );> > gfg.put(> 'Practice'> ,> 'practice .techcodeview.com> );> > gfg.put(> 'Code'> ,> 'code .techcodeview.com> );> > gfg.put(> 'Quiz'> ,> 'www .techcodeview.com> );> > > // looping over keys> > for> (String name : gfg.keySet())> > {> > // search for value> > String url = gfg.get(name);> > System.out.println(> 'Key = '> + name +> ', Value = '> + url);> > }> > }> }> |
Sortida:
Key = Quiz, Value = www.techcodeview.com Key = Practice, Value = practice.techcodeview.com Key = GFG, Value = techcodeview.com Key = Code, Value = code.techcodeview.com
Referències: Desbordament de pila