Java-Map从入门到性能分析1【Map初识、Map通用方法、HashMap的使用(keySet()、values、entrySet()、Iterator、性能分析)】

Map从入门到性能分析https://www.imooc.com/learn/1242【视频网址】

简介:Map是开发中,使用频率最高的知识点之一,Map家族也有很多成员,例如HashMap,LinkedMap等, 怎样更好的使用Map家族的这些成员,速度效率怎么更好的得以发挥,大谷老师将自己的一些经验与大家分享。

目   录

第1章 Map初识

1-1 Map初识(07:00)

1、可以学到什么?

2、课程目标

3、课程安排

4、编译器idea使用

1-2 Map通用方法(03:37)

1、Map接口及其实现类

2、Map接口通用方法

第2章 HashMap的使用

2-1 HashMap基本用法(08:42)

1、HashMap的构造方法

2、HashMap的基本用法

3、创建HashMap对象

2-2 HashMap的Entry结构(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、可以学到什么?

2、课程目标

遍历Map的方法有很多。哪种方法最合适、效率最高--->对比:性能分析===》合适的遍历

对Map进行优化--->需要知道底层原理

3、课程安排

有参构造方法、无参构造方法

4、编译器idea使用

jdk版本:1.8及以上!!!

创建项目

创建包

创建类

测试

下节课的课程安排!!!

1-2 Map通用方法(03:37)

1、Map接口及其实现类

Map是接口;HashMap、LinkedHashMap、TreeMap是3个实现类;

如箭头所示方向:下一级(LinkedHashMap)可以使用上一级(HashMap)的方法。 

LinkedHashMap可以使用HashMap的方法,HashMap可以使用Map的方法。

方法的重写、覆盖===》方法功能变化!!!

2、Map接口通用方法

存---put(键值对)、取---get()

k是标识---进行存取

containsKey():判断key是否存在。

  1. 往往与put()组合使用。【使用put()存value,可使用containsKey()判断key是否存在!】
  2. 用于查找Map中是否存在指定的kay-value。

第2章 HashMap的使用

2-1 HashMap基本用法(08:42)

1、HashMap的构造方法

根据 不同场景,选择 不同的构造方法 !!!

参数位置:根据不同场景、不同场合 计算得到!!!

2、HashMap的基本用法

设计啥类型,就 预先 指定啥类型。

3、创建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();
        //使用无参构造方法 创建 userMap
        Map<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结构(05:29)

源码解析(Entry结构)      4个变量(key、value、next、hash)      [ 键值映射、键值对、节点 ]

实际上:用put() 存储 键值映射,其实就是增加Entry。【一个键值映射 ---> 1个Entry对象】【存的是Entry、取的也是Entry】

不需要人工干预、人工操作Entry。内置方法 put()、get()自动往Entry中存取!方法的内部实际上是在调用Entry。

put()==》实际上是完成了4个变量(key、value、next、hash)的赋值工作!

next:下一个,Entry类型-->下一个Entry==》通过next找下一个Entry(键值映射)

hash:决定对next的哪个位置进行操作!

2-3 HashMap例题1(06:10)

根据 取值 与 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)

迭代器

加泛型,做数据类型的限定!【通过 map 获取 entrySet(),再 转换成 迭代器。】

it.hasNext() :判断迭代器是否还有下一个。

while()成立--->用 “ it.next() ” 取值(怎么存,怎么取!),将其转化为entry,通过 entry.getKey()、entry.getValues() 进行取值!

 

2-8 HashMap遍历性能分析1(08:00)

实验环境搭建:十万条记录-->进行测试!key越复杂,越接近真实环境,实验效果越真实!

2-9 HashMap遍历性能分析2(06:17)

相同条件下,进行测试!!!同样操作,同样目的,进行测试!!!

尽量避免使用keySet()。多使用Iterator、values! 

10万条记录!!!

2-10 HashMap遍历性能分析3(03:56)

计算机运行,CPU资源随机分配,由计算机系统自己进行调整!每次运行,有差异,在一定范围内求性能比。

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

    // 初始化Map
    public static Map inputMap() {
        //使用无参构造方法 创建 userMap
        Map<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++) {//十万的数据量--做性能分析
            int m = (int) (Math.random() * 10); // [0~9]
            //key = "x" + i;
            key = String.valueOf(str[m] + i * 100);//转换为字符类型
            value = i;//存储的时候,主要以key作为依据!!!
            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。循环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();//记录消耗时间---获取当前系统毫秒数
        Integer value;
        for (Integer v : userMap.values()) {
//            System.out.println(v + "===");
            value = v;
        }
        Long end = System.currentTimeMillis();//记录消耗时间---获取当前系统毫秒数
        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()进行获取】
        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));
    }

}

 希望对您有所帮助~~~

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章