酷知百科網

位置:首頁 > 職場理財 > 職場就業

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)

map集合的遍歷方式

(02)Map<String,Integer>maps=new HashMap<String,Integer>();獲得鍵值對的value值,組成集合,之所以使用collection集合,沒有爲什麼,看API,返回的是Collection集合,所以使用collection,很多東西在API裏面都有,在以後學習過程中要是善於使用API文檔解決問題,多讀讀。Collection <Integer>c=e();

map集合的遍歷方式 第2張

(03)map集合中value值有可能是重複的,使用這種方式可以獲得value值,但是不能獲得keyfor(Integer i:c)

map集合的遍歷方式 第3張

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

map集合的遍歷方式 第4張

(05)//第三種:推薦,尤其是容量大時    tln("透過ySet遍歷key和value");    for (y<Integer, String> entry : ySet()) {     tln("key= " + ey() + " and value= " + alue());    }

map集合的遍歷方式 第5張

(06)tln("透過ySet使用iterator遍歷key和value:");    //迭代器模式   把集合中的數據  臨時用迭代器的表示    Iterator<y<Integer, String>> it = ySet()ator();    while (ext())

map集合的遍歷方式 第6張

特別提示

推薦使用Setsets=yset();

標籤:map 遍歷