map集合的遍歷方式
map集合內的元素key值不能相同,value值可以相同;hashmap經常和map一起使用
Map<String ,Integer>maps=new HashMap<String,String>();
操作方法
(01)map集合的遍歷方式1,通過獲得key值遍歷map集合Map<String ,Integer> maps=new HashMap<String,Integer>();String [] strs=et();for(String s:strs)
(02)Map<String,Integer>maps=new HashMap<String,Integer>();獲得鍵值對的value值,組成集合,之所以使用collection集合,沒有為什麼,看API,返回的是Collection集合,所以使用collection,很多東西在API裏面都有,在以後學習過程中要是善於使用API文檔解決問題,多讀讀。Collection <Integer>c=e();
(03)map集合中value值有可能是重複的,使用這種方式可以獲得value值,但是不能獲得keyfor(Integer i:c)
(04)Map<String,Integer>=new HashMap<String,Integer>();//獲得鍵值對集合,Set<y<String,Integer>>sets=yset();for(y<String,Integer e:sets>){tln("key="+ey()+",value="+alue())}
(05)//第三種:推薦,尤其是容量大時 tln("通過ySet遍歷key和value"); for (y<Integer, String> entry : ySet()) { tln("key= " + ey() + " and value= " + alue()); }
(06)tln("通過ySet使用iterator遍歷key和value:"); //迭代器模式 把集合中的數據 臨時用迭代器的表示 Iterator<y<Integer, String>> it = ySet()ator(); while (ext())
特別提示
推薦使用Setsets=yset();