如何在Java中迭代地图或者HashMap
时间:2020-02-23 14:34:21 来源:igfitidea点击:
在本教程中,我们将看到我们如何在Java中迭代地图。
有四种方法可以迭代地图,HashMap或者Treemap。
如果在迭代时删除元素,则第1和第3个选项将抛出java.util.concurrentmodificationException。
如果我们了解HashMap的内部工作,那么我们可能更容易迭代HashMap
让我们举个例子:
- iteratemapmain.java.
package org.igi.theitroad; import java.util.HashMap; import java.util.Iterator; import java.util.Map.Entry; public class IterateMapMain { public static void main(String args[]) { //HashMap with Country as key and capital as value HashMap<String,String> countryCapitalMap=new HashMap<String,String>(); countryCapitalMap.put("Netherlands","Delhi"); countryCapitalMap.put("Japan","Tokyo"); countryCapitalMap.put("France","Paris"); countryCapitalMap.put("Russia","Moscow"); //Iterating Using keySet() and for each loop System.out.println("Iterating Using keySet() and for each loop"); for (String countryKey:countryCapitalMap.keySet()) { System.out.println("Country:"+ countryKey +" and Capital:"+countryCapitalMap.get(countryKey)); } System.out.println("-----------------------------"); //Iterating Using keySet() and java iterator System.out.println("Iterating Using keySet() and java Iterator"); Iterator countryKeySetIterator=countryCapitalMap.keySet().iterator(); while(countryKeySetIterator.hasNext()){ String countryKey=countryKeySetIterator.next(); System.out.println("Country:"+ countryKey +" and Capital:"+countryCapitalMap.get(countryKey)); } System.out.println("-----------------------------"); //Iterating Using entrySet() and for each loop System.out.println("Iterating Using entrySet() and for each loop"); for (Entry<String,String> entry:countryCapitalMap.entrySet()) { System.out.println("Country:"+ entry.getKey() +" and Capital:"+entry.getValue()); } System.out.println("-----------------------------"); //Iterating Using entrySet() and java iterator System.out.println("Iterating Using entrySet() and and java Iterator"); Iterator<Entry<String,String>> entryIterator=countryCapitalMap.entrySet().iterator(); while(entryIterator.hasNext()) { Entry<String,String> entry=entryIterator.next(); System.out.println("Country:"+ entry.getKey() +" and Capital:"+entry.getValue()); } System.out.println("-----------------------------"); } }
运行它,我们将获取以下输出:
Iterating Using keySet() and for each loop Country:France and Capital:Paris Country:Russia and Capital:Moscow Country:Japan and Capital:Tokyo Country:Netherlands and Capital:Delhi ---------------------------- Iterating Using keySet() and java Iterator Country:France and Capital:Paris Country:Russia and Capital:Moscow Country:Japan and Capital:Tokyo Country:Netherlands and Capital:Delhi ---------------------------- Iterating Using entrySet() and for each loop Country:France and Capital:Paris Country:Russia and Capital:Moscow Country:Japan and Capital:Tokyo Country:Netherlands and Capital:Delhi ---------------------------- Iterating Using entrySet() and and java Iterator Country:France and Capital:Paris Country:Russia and Capital:Moscow Country:Japan and Capital:Tokyo Country:Netherlands and Capital:Delhi ----------------------------