• Map、Set、List初始化大小的影响


    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.HashSet;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    public class Test
    {
    
        public static void main(String[] args)
        {
    	testHashMap(16);
    	testHashMap(5000000);
    	System.out.println("----------------");
    	testHashSet(16);
    	testHashSet(5000000);
    	System.out.println("----------------");
    	testArrayList(16);
    	testArrayList(5000000);
        }
    
        public static void testHashMap(int initSize)
        {
    	Map<Integer, String> map = new HashMap<Integer, String>(initSize);
    
    	long beginTime = System.currentTimeMillis();
    	for (int i = 0; i < 5000000; i++)
    	{
    	    map.put(i, "hellooiuytrewq");
    	}
    	long endTime = System.currentTimeMillis();
    	System.out.println(endTime - beginTime);
        }
    
        public static void testHashSet(int initSize)
        {
    	Set<Integer> set = new HashSet<Integer>(initSize);
    
    	long beginTime = System.currentTimeMillis();
    	for (int i = 0; i < 5000000; i++)
    	{
    	    set.add(i);
    	}
    	long endTime = System.currentTimeMillis();
    	System.out.println(endTime - beginTime);
        }
    
        public static void testArrayList(int initSize)
        {
    	List<Integer> list = new ArrayList<Integer>(initSize);
    
    	long beginTime = System.currentTimeMillis();
    	for (int i = 0; i < 5000000; i++)
    	{
    	    list.add(i);
    	}
    	long endTime = System.currentTimeMillis();
    	System.out.println(endTime - beginTime);
        }
    
    }
    

      

    4329
    2184
    ----------------
    2832
    2088
    ----------------
    1185
    451

  • 相关阅读:
    【转载】Chrome 0day漏洞:不要用Chrome查看pdf文件
    内网渗透闲谈
    Sticky Fingure安装教程
    局域网下交换机配置
    Access 2010 应用基础 单元三:SQL查询
    ESP8266 wifi干扰、钓鱼实现
    这是谁的锅
    svn/git的diff、patch
    sublime开发php必备工具集合(mac)
    gcc学习笔记
  • 原文地址:https://www.cnblogs.com/mu-tou-man/p/5173949.html
Copyright © 2020-2023  润新知