Java-Map从入门到性能分析1【Map初识、Map通用方法、HashMap的使用(遍历方法、性能分析)】
【視頻網(wǎng)址】:慕課網(wǎng)——Map從入門到性能分析
簡介:Map是開發(fā)中,使用頻率最高的知識點之一,Map家族也有很多成員,例如HashMap,LinkedMap等, 怎樣更好地使用Map家族的這些成員,速度效率怎么更好地得以發(fā)揮,我將自己的一些經(jīng)驗與大家分享。
- 筆記01🚀【第1、2章】
- 筆記02🚀【第3章】
- 筆記03🚀【第4、5、6章】
工程文件【鏈接:https://pan.baidu.com/s/1Vg3FLVtGeCCKZ3R68Xi2tA【提取碼:zjxs】
目? ?錄
第1章 Map初識
1-1 Map初識(07:00)
1、可以學(xué)到什么?
2、課程目標(biāo)
3、課程安排
4、編譯器idea使用
1-2 Map通用方法(03:37)
1、Map接口及其實現(xiàn)類
2、Map接口通用方法
第2章 HashMap的使用
2-1 HashMap基本用法(08:42)
1、HashMap的構(gòu)造方法
2、HashMap的基本用法
3、創(chuàng)建HashMap對象
2-2 HashMap的Entry結(jié)構(gòu)(05:29)
2-3 HashMap例題1(06:10)
2-4 HashMap遍歷-keySet(06:09)
2-5 HashMap遍歷-values(02:57)
2-6 HashMap遍歷-entrySet(04:34)
2-7 HashMap遍歷-lterator(04:44)
2-8 HashMap遍歷性能分析1(08:00)
2-9?HashMap遍歷性能分析2(06:17)
2-10?HashMap遍歷性能分析3(03:56)
1、?50萬條記錄!!!
2、100萬條記錄!!!
3、500萬條記錄!!!
4、1000萬條記錄!!!
代碼匯總
第1章 Map初識
1-1 Map初識(07:00)
1、可以學(xué)到什么?
2、課程目標(biāo)
遍歷Map的方法有很多。哪種方法最合適、效率最高--->對比:性能分析===》合適的遍歷
對Map進(jìn)行優(yōu)化--->需要知道底層原理
3、課程安排
有參構(gòu)造方法、無參構(gòu)造方法
4、編譯器idea使用
jdk版本:1.8及以上!!!
創(chuàng)建項目
創(chuàng)建包
創(chuàng)建類
測試
下節(jié)課的課程安排!!!
1-2 Map通用方法(03:37)
1、Map接口及其實現(xiàn)類
Map是接口;HashMap、LinkedHashMap、TreeMap是3個實現(xiàn)類;
如箭頭所示方向:下一級(LinkedHashMap)可以使用上一級(HashMap)的方法。?
LinkedHashMap可以使用HashMap的方法,HashMap可以使用Map的方法。
方法的重寫、覆蓋===》方法功能變化!!!
2、Map接口通用方法
存---put(鍵值對)、取---get()
k是標(biāo)識---進(jìn)行存取
containsKey():判斷key是否存在。
第2章 HashMap的使用
2-1 HashMap基本用法(08:42)
1、HashMap的構(gòu)造方法
根據(jù) 不同場景,選擇 不同的構(gòu)造方法 !!!
參數(shù)位置:根據(jù)不同場景、不同場合 計算得到!!!
2、HashMap的基本用法
設(shè)計啥類型,就?預(yù)先?指定啥類型。
3、創(chuàng)建HashMap對象
package com.imooc;import java.util.HashMap; import java.util.Map;public class TestMap {public static void main(String[] args) { // System.out.println("加油~~~");Map one = new HashMap();//使用無參構(gòu)造方法 創(chuàng)建 userMapMap<String, Integer> userMap = new HashMap<String, Integer>();userMap.put("zhangsan1", 110);userMap.put("zhangsan2", 120);userMap.put("zhangsan3", 130);Integer n = userMap.get("zhangsan2");System.out.println(n + "====");} }2-2 HashMap的Entry結(jié)構(gòu)(05:29)
源碼解析(Entry結(jié)構(gòu))? ?? ?4個變量(key、value、next、hash)? ? ? [ 鍵值映射、鍵值對、節(jié)點 ]
實際上:用put() 存儲 鍵值映射,其實就是增加Entry。【一個鍵值映射 ---> 1個Entry對象】【存的是Entry、取的也是Entry】
不需要人工干預(yù)、人工操作Entry。內(nèi)置方法 put()、get()自動往Entry中存取!方法的內(nèi)部實際上是在調(diào)用Entry。
put()==》實際上是完成了4個變量(key、value、next、hash)的賦值工作!
next:下一個,Entry類型-->下一個Entry==》通過next找下一個Entry(鍵值映射)
hash:決定對next的哪個位置進(jìn)行操作!
2-3 HashMap例題1(06:10)
根據(jù) 取值 與 hash 決定 輸出順序!
2-4 HashMap遍歷-keySet(06:09)
2-5 HashMap遍歷-values(02:57)
只能獲取map的value!!!
2-6 HashMap遍歷-entrySet(04:34)
2-7 HashMap遍歷-lterator(04:44)
迭代器
加泛型,做數(shù)據(jù)類型的限定!【通過 map 獲取 entrySet(),再 轉(zhuǎn)換成 迭代器。】
it.hasNext() :判斷迭代器是否還有下一個。
while()成立--->用 “ it.next() ” 取值(怎么存,怎么取!),將其轉(zhuǎn)化為entry,通過 entry.getKey()、entry.getValues() 進(jìn)行取值!
?
2-8 HashMap遍歷性能分析1(08:00)
實驗環(huán)境搭建:十萬條記錄-->進(jìn)行測試!key越復(fù)雜,越接近真實環(huán)境,實驗效果越真實!
2-9?HashMap遍歷性能分析2(06:17)
相同條件下,進(jìn)行測試!!!同樣操作,同樣目的,進(jìn)行測試!!!
盡量避免使用keySet()。多使用Iterator、values!?
10萬條記錄!!!
2-10?HashMap遍歷性能分析3(03:56)
計算機(jī)運行,CPU資源隨機(jī)分配,由計算機(jī)系統(tǒng)自己進(jìn)行調(diào)整!每次運行,有差異,在一定范圍內(nèi)求性能比。
1、?50萬條記錄!!!
2、100萬條記錄!!!
3、500萬條記錄!!!
4、1000萬條記錄!!!
代碼匯總
package com.imooc;import java.util.HashMap; import java.util.Iterator; import java.util.Map;public class TestMap {public static void main(String[] args) {//System.out.println("加油~~~");Map one = new HashMap();//Integer n = userMap.get("zhangsan2");//System.out.println(n + "====");//System.out.println(userMap);Map map1 = inputMap();System.out.println(map1);System.out.println("-------------------------------");showMap1(map1);System.out.println("-------------------------------");showMap2(map1);System.out.println("-------------------------------");showMap3(map1);System.out.println("-------------------------------");showMap4(map1);}// 初始化Mappublic static Map inputMap() {//使用無參構(gòu)造方法 創(chuàng)建 userMapMap<String, Integer> userMap = new HashMap<String, Integer>(); // userMap.put("zhangsan1", 110); // userMap.put("zhangsan2", 120); // userMap.put("zhangsan3", 130); // userMap.put("zhangsan4", 140); // userMap.put("zhangsan5", 150);String str[] = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j"};String key;Integer value;for (int i = 0; i < 10000000; i++) {//十萬的數(shù)據(jù)量--做性能分析int m = (int) (Math.random() * 10); // [0~9]//key = "x" + i;key = String.valueOf(str[m] + i * 100);//轉(zhuǎn)換為字符類型value = i;//存儲的時候,主要以key作為依據(jù)!!!userMap.put(key, value);}return userMap;}/*** 1、利用keySet()遍歷Map* 好處:可以獲取到key本身、key代表的value** @param userMap*/public static void showMap1(Map<String, Integer> userMap) {//Map形參需要加上泛型Long start = System.currentTimeMillis();//map.keySet():從map對象中獲取key。循環(huán)1次,獲取1個key,存到冒號左邊的key變量中Integer value;for (String key : userMap.keySet()) { // System.out.println(key + "***" + userMap.get(key));value = userMap.get(key);}Long end = System.currentTimeMillis();System.out.println("1、keySet:" + (end - start));}/**** 2、利用values遍歷Map* @param userMap*/public static void showMap2(Map<String, Integer> userMap) {Long start = System.currentTimeMillis();//記錄消耗時間---獲取當(dāng)前系統(tǒng)毫秒數(shù)Integer value;for (Integer v : userMap.values()) { // System.out.println(v + "===");value = v;}Long end = System.currentTimeMillis();//記錄消耗時間---獲取當(dāng)前系統(tǒng)毫秒數(shù)System.out.println("2、values:" + (end - start));}/**** 3、利用entrySet()遍歷Map* @param userMap*/public static void showMap3(Map<String, Integer> userMap) {Long start = System.currentTimeMillis();//Map.Entry<String, Integer>類型的變量//一次性全部獲取key、value【entrySet()更快】【keySet()獲取value需要使用get()進(jìn)行獲取】Integer value;for (Map.Entry<String, Integer> entry : userMap.entrySet()) { // System.out.println(entry); // System.out.println(entry.getKey() + "===" + entry.getValue());value = entry.getValue();}Long end = System.currentTimeMillis();System.out.println("3、entrySet:" + (end - start));}/**** 4、利用Iterator遍歷Map* @param userMap*/public static void showMap4(Map<String, Integer> userMap) {Long start = System.currentTimeMillis();Iterator<Map.Entry<String, Integer>> it = userMap.entrySet().iterator();Integer value;while (it.hasNext()) {Map.Entry<String, Integer> entry = it.next(); // System.out.println(entry.getKey() + "===" + entry.getValue());value = entry.getValue();}Long end = System.currentTimeMillis();System.out.println("4、Iterator:" + (end - start));}}?希望對您有所幫助~~~
總結(jié)
以上是生活随笔為你收集整理的Java-Map从入门到性能分析1【Map初识、Map通用方法、HashMap的使用(遍历方法、性能分析)】的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android基础---学习历程【上课用
- 下一篇: java美元兑换,(Java实现) 美元