Java讀取Map的兩種方法與對(duì)比
前言
在java中遍歷Map有不少的方法。這篇文章我們就來(lái)看一下Java讀取Map的兩種方法以及這兩種方法的對(duì)比。
一、 遍歷Map方法A
Map map = new HashMap();
Iterator iter = map.entrySet().iterator();
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry) iter.next();
Object key = entry.getKey();
Object val = entry.getValue();
}
二、遍歷Map方法B
Map map = new HashMap();
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
Object key = iter.next();
Object val = map.get(key);
}
三、分析遍歷方法
方法A: 在遍歷中一次讀取Map.Entry,然后直接獲取的值。
方法B: 基于keySet則是,先遍歷,然后再?gòu)腗ap中讀取信息。
四、性能測(cè)試
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.junit.BeforeClass;
import org.junit.Test;
public class MapLoopA {
private static Map<Integer, String> infos = new HashMap<Integer, String>();
@BeforeClass
public static void setUp() {
for (int i=0; i<1000000; i++) {
infos.put(i, "test information" + i);
}
System.out.println("setUp is done.");
}
@Test
public void testMapLoopA() {
Iterator<Map.Entry<Integer, String>> iterator = infos.entrySet().iterator();
long startTime = System.currentTimeMillis();
while (iterator.hasNext()) {
Map.Entry<Integer, String> entry = iterator.next();
int key = entry.getKey();
String val = entry.getValue();
}
System.out.println("A solution takes in looping Map with 1000000 entries:"
+ (System.currentTimeMillis()-startTime) + " milli seconds");
}
@Test
public void testMapLoopB() {
Iterator<Integer> iterator = infos.keySet().iterator();
long startTime = System.currentTimeMillis();
while (iterator.hasNext()) {
int key = iterator.next();
String val = infos.get(key);
}
System.out.println("B solution takes in looping Map with 1000000 entries:" +
(System.currentTimeMillis()-startTime) + " milli seconds");
}
}
測(cè)試結(jié)果:
由此可見,在Map中存放1000000個(gè)數(shù)據(jù),并在此數(shù)據(jù)集合中,進(jìn)行遍歷。效率上差異將近1倍的性能差異。
五、總結(jié)
好了,以上就是這篇文章的全部?jī)?nèi)容了,可以看車方法A的效率總體要高一些。一般推薦大家使用方法A。希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來(lái)一定的幫助。
相關(guān)文章
java實(shí)現(xiàn)識(shí)別二維碼圖片功能
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)識(shí)別二維碼圖片功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
Java使用JNDI連接數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法
本文主要介紹了Java使用JNDI連接數(shù)據(jù)庫(kù)的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-12-12
SpringBoot對(duì)數(shù)據(jù)訪問層進(jìn)行單元測(cè)試的方法詳解
我們公司作為一個(gè)面向銀行、金融機(jī)構(gòu)的TO B類企業(yè),頻繁遇到各個(gè)甲方爸爸提出的國(guó)產(chǎn)化數(shù)據(jù)庫(kù)的改造需求,包括OceanBase, TiDB,geldenDB等等,本文就介紹一種快高效、可復(fù)用的解決方案——對(duì)數(shù)據(jù)訪問層做單元測(cè)試,需要的朋友可以參考下2023-08-08
JavaBean和SpringBean的區(qū)別及創(chuàng)建SpringBean方式
這篇文章主要介紹了JavaBean和SpringBean的區(qū)別及創(chuàng)建SpringBean方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Jlabel實(shí)現(xiàn)內(nèi)容自動(dòng)換行簡(jiǎn)單實(shí)例
這篇文章主要介紹了Jlabel實(shí)現(xiàn)內(nèi)容自動(dòng)換行簡(jiǎn)單實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-01-01
基于Mybatis-plus實(shí)現(xiàn)多租戶架構(gòu)的全過程
多租戶是一種軟件架構(gòu)技術(shù),在多用戶的環(huán)境下,共有同一套系統(tǒng),并且要注意數(shù)據(jù)之間的隔離性,下面這篇文章主要給大家介紹了關(guān)于基于Mybatis-plus實(shí)現(xiàn)多租戶架構(gòu)的相關(guān)資料,需要的朋友可以參考下2022-02-02

