参考链接:https://www.cnblogs.com/crazyacking/p/5573528.html
==================================
java紫色代表迭代方式
效率测试:100W
==================================
HashMap迭代方式1:entrySet迭代
public static void main(String[] args) { MaphashMap = new HashMap<>(); long beginTime = System.currentTimeMillis(); System.out.println("hashMap存储开始时间-->"+beginTime); for (int i = 0; i < 1000000; i++) { hashMap.put(UUID.randomUUID().toString(),UUID.randomUUID().toString()); } long endTime = System.currentTimeMillis(); System.out.println("hashMap存储结束时间-->"+endTime); System.out.println("hashMap存储消耗:"+(endTime-beginTime)+"ms"); System.out.println("hashMap【entrySet方式】读取开始时间-->"+endTime); //可以使用外部定义变量 for (Map.Entry entry : hashMap.entrySet()){ System.out.println(entry.getKey() + ":" + entry.getValue()); } long endTime2 = System.currentTimeMillis(); System.out.println("hashMap【entrySet方式】读取结束时间-->"+endTime2); System.out.println("hashMap【entrySet方式】读取消耗:"+(endTime2-endTime)+"ms"); }
===================================================================
HashMap迭代方式2:keySet迭代
public static void main(String[] args) { MaphashMap = new HashMap<>(); long beginTime = System.currentTimeMillis(); System.out.println("hashMap存储开始时间-->"+beginTime); for (int i = 0; i < 1000000; i++) { hashMap.put(UUID.randomUUID().toString(),UUID.randomUUID().toString()); } long endTime = System.currentTimeMillis(); System.out.println("hashMap存储结束时间-->"+endTime); System.out.println("hashMap存储消耗:"+(endTime-beginTime)+"ms"); System.out.println("hashMap【keySet方式】读取开始时间-->"+endTime); //可以使用外部定义变量 Set keySet = hashMap.keySet(); for (String s : keySet) { System.out.println(s+":"+hashMap.get(s)); } long endTime2 = System.currentTimeMillis(); System.out.println("hashMap【keySet方式】读取结束时间-->"+endTime2); System.out.println("hashMap【keySet方式】读取消耗:"+(endTime2-endTime)+"ms"); }
=================================================================
HashMap迭代方式3:forEach方式
public static void main(String[] args) { MaphashMap = new HashMap<>(); long beginTime = System.currentTimeMillis(); System.out.println("hashMap存储开始时间-->"+beginTime); for (int i = 0; i < 1000000; i++) { hashMap.put(UUID.randomUUID().toString(),UUID.randomUUID().toString()); } long endTime = System.currentTimeMillis(); System.out.println("hashMap存储结束时间-->"+endTime); System.out.println("hashMap存储消耗:"+(endTime-beginTime)+"ms"); System.out.println("hashMap【forEach方式】读取开始时间-->"+endTime); //不能使用外部定义变量 除非final类型 例如:List hashMap.forEach((k,v)->{ System.out.println(k +":"+v); }); long endTime2 = System.currentTimeMillis(); System.out.println("hashMap【forEach方式】读取结束时间-->"+endTime2); System.out.println("hashMap【forEach方式】读取消耗:"+(endTime2-endTime)+"ms"); }
=======================================================================================================
TreeMap迭代:entrySet方式
public static void main(String[] args) { MaphashMap = new TreeMap<>(); long beginTime = System.currentTimeMillis(); System.out.println("TreeMap存储开始时间-->"+beginTime); for (int i = 0; i < 1000000; i++) { hashMap.put(UUID.randomUUID().toString(),UUID.randomUUID().toString()); } long endTime = System.currentTimeMillis(); System.out.println("TreeMap存储结束时间-->"+endTime); System.out.println("TreeMap存储消耗:"+(endTime-beginTime)+"ms"); System.out.println("TreeMap【entrySet方式】读取开始时间-->"+endTime); //可以使用外部定义变量 for (Map.Entry entry : hashMap.entrySet()){ System.out.println(entry.getKey() + ":" + entry.getValue()); } long endTime2 = System.currentTimeMillis(); System.out.println("TreeMap【entrySet方式】读取结束时间-->"+endTime2); System.out.println("TreeMap【entrySet方式】读取消耗:"+(endTime2-endTime)+"ms"); }
=================================================
TreeMap迭代:keySet方式
public static void main(String[] args) { MaphashMap = new TreeMap<>(); long beginTime = System.currentTimeMillis(); System.out.println("TreeMap存储开始时间-->"+beginTime); for (int i = 0; i < 1000000; i++) { hashMap.put(UUID.randomUUID().toString(),UUID.randomUUID().toString()); } long endTime = System.currentTimeMillis(); System.out.println("TreeMap存储结束时间-->"+endTime); System.out.println("TreeMap存储消耗:"+(endTime-beginTime)+"ms"); System.out.println("TreeMap【keySet方式】读取开始时间-->"+endTime); //可以使用外部定义变量 Set keySet = hashMap.keySet(); for (String s : keySet) { System.out.println(s+":"+hashMap.get(s)); } long endTime2 = System.currentTimeMillis(); System.out.println("TreeMap【keySet方式】读取结束时间-->"+endTime2); System.out.println("TreeMap【keySet方式】读取消耗:"+(endTime2-endTime)+"ms"); }
==========================================================
TreeMap迭代:forEach方式
public static void main(String[] args) { MaphashMap = new TreeMap<>(); long beginTime = System.currentTimeMillis(); System.out.println("TreeMap存储开始时间-->"+beginTime); for (int i = 0; i < 1000000; i++) { hashMap.put(UUID.randomUUID().toString(),UUID.randomUUID().toString()); } long endTime = System.currentTimeMillis(); System.out.println("TreeMap存储结束时间-->"+endTime); System.out.println("TreeMap存储消耗:"+(endTime-beginTime)+"ms"); System.out.println("TreeMap【forEach方式】读取开始时间-->"+endTime); //不能使用外部定义变量 除非final类型 例如:List hashMap.forEach((k,v)->{ System.out.println(k +":"+v); }); long endTime2 = System.currentTimeMillis(); System.out.println("TreeMap【forEach方式】读取结束时间-->"+endTime2); System.out.println("TreeMap【forEach方式】读取消耗:"+(endTime2-endTime)+"ms"); }
=========================================================
100W级别
基本判断【并不准确】:HashMap存储效率高,读取效率也比较高
TreeMap存储效率低,读取效率差不多
=========================================================
1000W级别