HashMap的三種遍歷方式

2021-09-25 07:51:52 字數 1001 閱讀 3477

public class testhashmap 

//第一種方式:通過遍歷map.keyset()遍歷hashmap的key和value

firstmethod(map);

//第二種方式:通過遍歷values()遍歷map的value,但是不能遍歷key

secondmethod1(map);

secondmethod2(map);

//第三種方式:通過map.entryset()使用iterator()遍歷hashmap的key和value

thirdmethod1(map);

thirdmethod2(map);

}private static void firstmethod(mapmap)

system.out.println("第一種耗時:"+(system.currenttimemillis() - starttime));

}private static void secondmethod1(mapmap)

system.out.println("第二種耗時:"+(system.currenttimemillis() - starttime));

}private static void secondmethod2(mapmap)

system.out.println("第二種耗時:"+(system.currenttimemillis() - starttime));

}private static void thirdmethod1(mapmap)

system.out.println("第三種耗時:"+(system.currenttimemillis() - starttime));

}private static void thirdmethod2(mapmap)

system.out.println("第三種耗時:"+(system.currenttimemillis() - starttime));

}}

遍歷HashMap的三種方式

方式一 entryset 遍歷hashmap的entryset鍵值對集合 1.通過hashmap.entryset 得到鍵值對集合 2.通過迭代器iterator遍歷鍵值對集合得到key值和value值 1 test 2public void testhashmap01 16 17輸出結果 18 k...

HashMap的三種遍歷方法

mapmap new hashmap map.put 阿里巴巴 電子商務 方法一 對map的儲存結構比較了解時就能想到這種方法 set set map.entryset for iterator it set.iterator it.hasnext 方法二 只能得到value值,不能得到key co...

HashMap的三種遍歷方法

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