zoukankan      html  css  js  c++  java
  • Java学习笔记(1)----规则集和线性表性能比较

      为了比较 HashSet,LinkedHashSet,TreeSet,ArrayList,LinkedList 的性能,使用如下代码来测试它们加入并删除500000个数据的时间:

    package src;
    
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Collection;
    import java.util.Collections;
    import java.util.HashSet;
    import java.util.LinkedHashSet;
    import java.util.LinkedList;
    import java.util.TreeSet;
    
    
    
    
    public class Solution {
    
        
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            Collection<Integer>set1=new HashSet<Integer>();
            System.out.println("Time for hash set is "+getTestTime(set1,500000)+" milliseconds");
            
            Collection<Integer>set2=new LinkedHashSet<Integer>();
            System.out.println("Time for linked hash set is "+getTestTime(set2, 500000)+" milliseconds");
            
            Collection<Integer>set3=new TreeSet<Integer>();
            System.out.println("Time for tree set is "+getTestTime(set3, 500000)+" milliseconds");
            
            Collection<Integer>set4=new ArrayList<Integer>();
            System.out.println("Time for arraylist is "+getTestTime(set4, 500000)+" milliseconds");
            
            Collection<Integer>set5=new LinkedList<Integer>();
            System.out.println("Time for linked list is "+getTestTime(set5, 500000)+" milliseconds");
            
            System.out.println("
    Game Over!");
        }
        
        public static long getTestTime(Collection<Integer>c,int size){
            long startTime=System.currentTimeMillis();
            List<Integer>list=new ArrayList<Integer>();
            for(int i=0;i<size;i++){
                list.add(i);
            }
            
            Collections.shuffle(list);
            for(int ele:list){
                c.add(ele);
            }
            
            Collections.shuffle(list);
            
            for(int ele:list){
                c.remove(ele);
            }
            
            long endTime=System.currentTimeMillis();
            return endTime-startTime;
        }
    }

    运行结果如下:

    Time for hash set is 372 milliseconds
    Time for linked hash set is 404 milliseconds
    Time for tree set is 936 milliseconds
    Time for arraylist is 200759 milliseconds
    Time for linked list is 473436 milliseconds
    
    Game Over!

    可以看到,速度由快到慢依次是:HashSet,LinkedHashSet,TreeSet,ArrayList,LinkedList。

  • 相关阅读:
    3 * 0.1 == 0.3 将会返回什么?true 还是 false?
    Java中存储金额用什么数据类型?
    oracle数据库中索引失效的几种情况
    MyBatis如何防止SQL注入
    Windows10连接到内网(局域网)段
    Linux上安装Tomcat并启动时报Cannot find /usr/local/tomcat/tomcat_8080/bin/setclasspath.sh
    Linux上安装Mysql
    Linux上安装JDK
    FileZilla的使用和注意事项
    Failure to find parent:pom:2.2.6 in http://maven.aliyun was cached in the local repository...
  • 原文地址:https://www.cnblogs.com/dongling/p/5741709.html
Copyright © 2011-2022 走看看