HashMap的三種遍歷方法

2021-07-24 11:06:26 字數 591 閱讀 4381

mapmap = new hashmap();  

map.put("阿里巴巴", "電子商務");

/**

* 方法一:對map的儲存結構比較了解時就能想到這種方法

*/

set> set = map.entryset();

for(iterator> it = set.iterator();it.hasnext();)

/**

* 方法二:只能得到value值,不能得到key

*/

collectioncollection = map.values();

for(iteratorit = collection.iterator();it.hasnext();)

/**

* 方法三:常用的一種方法,首先得到key的集合,遍歷集合的每乙個元素,再通過map的get方法得到value

*/

setkeyset = map.keyset();

for(iteratorit = keyset.iterator();it.hasnext();)

HashMap的三種遍歷方法

最常規的一種遍歷方法,最常規就是最常用的,雖然不複雜,但很重要,這是我們最熟悉的,就不多說了!public static void work mapmap 利用keyset進行遍歷,它的優點在於可以根據你所想要的key值得到你想要的 values,更具靈活性!public static void w...

HashMap的三種遍歷方法

hashmap儲存的是鍵值對,可以將key序列和value序列單獨抽取出來。hashmap的遍歷方法 第一種 抽取出key序列,將map中的所有key生成乙個set。hashmapmap new hashmap setkeys map.keyset 返回的是乙個set然後得到它的迭代器,遍歷元素。如...

HashMap的三種遍歷方式

public class testhashmap 第一種方式 通過遍歷map.keyset 遍歷hashmap的key和value firstmethod map 第二種方式 通過遍歷values 遍歷map的value,但是不能遍歷key secondmethod1 map secondmetho...