zoukankan      html  css  js  c++  java
  • java普通for循环和for-each迭代循环的区别

    PO实体类User:

    package aA;
    
    
    public class User {
        private String name;
        
        private int many;
        private int id;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public int getMany() {
            return many;
        }
    
        public void setMany(int i) {
            this.many = i;
        }
        public User(int many,int id){
            this.many = many;
            this.id  =id;
        }
        public User(){
            
        }
    }

    PO实体类Cumster:

    package aA;
    
    public class Cumster {
        private String name;
        private int id;
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        private int many;
        private int age;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getMany() {
            return many;
        }
        public void setMany(int many) {
            this.many = many;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
        public Cumster(String name,int id){
            this.name = name;
            this.id = id;
        }
        public Cumster(){
            
        }
    }

    随机产生字符串(模拟list数据添加):

    package aA;
    
    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Random;
    
    /**
     * 字符随机生成类
     */
    public class RandomDemo {
        /**
         * 随机产生类型枚举
         */
        public static enum TYPE {
            /**小字符型*/
            LETTER,
            /**大写字符型*/
            CAPITAL,
            /**数字型*/
            NUMBER,
            /**大+小字符 型*/
            LETTER_CAPITAL,
            /**小字符+数字 型*/
            LETTER_NUMBER,
            /**大写字符+数字*/
            CAPITAL_NUMBER,
            /**大+小字符+数字 型*/
            LETTER_CAPITAL_NUMBER,
        }
        private static String[] lowercase = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
                "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z" };
    
        private static String[] capital = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P",
                "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
    
        private static String[] number = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
    
        /**
         * 静态随机数
         */
        private static Random random = new Random();
    
        /**
         * 获取随机组合码
         * @param num 位数
         * @param type 类型
         * @type 
         * <br>小写字符型 LETTER,
         * <br>大写字符型 CAPITAL,
         * <br>数字型 NUMBER,
         * <br>大+小字符型 LETTER_CAPITAL,
         * <br>小字符+数字 型 LETTER_NUMBER,
         * <br>大字符+数字 型 CAPITAL_NUMBER,
         * <br>大+小字符+数字 型 LETTER_CAPITAL_NUMBER,
         */
        public static String getRandom(int num, TYPE type) {
            ArrayList<String> temp = new ArrayList<String>();
            StringBuffer code = new StringBuffer();
            switch (type) {
            case LETTER:
                temp.addAll(Arrays.asList(lowercase));
                break;
            case CAPITAL:
                temp.addAll(Arrays.asList(capital));
                break;
            case NUMBER:
                temp.addAll(Arrays.asList(number));
                break;
            case LETTER_CAPITAL:
                temp.addAll(Arrays.asList(lowercase));
                temp.addAll(Arrays.asList(capital));
                break;
            case LETTER_NUMBER:
                temp.addAll(Arrays.asList(lowercase));
                temp.addAll(Arrays.asList(number));
                break;
            case CAPITAL_NUMBER:
                temp.addAll(Arrays.asList(capital));
                temp.addAll(Arrays.asList(number));
                break;
            case LETTER_CAPITAL_NUMBER:
                temp.addAll(Arrays.asList(lowercase));
                temp.addAll(Arrays.asList(capital));
                temp.addAll(Arrays.asList(number));
                break;
            }
            for (int i = 0; i < num; i++) {
                code.append(temp.get(random.nextInt(temp.size())));
            }
            return code.toString();
        }
    //    public static void main(String[] args) {
    //        System.out.println(RandomDemo.getRandom(10, RandomDemo.TYPE.LETTER_CAPITAL));
    //    }
    }

    测试类:

    package aA;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    import java.util.Random;
    
    public class AA {
    
        public static void main(String[] args) {
            List<User> userList = new ArrayList<User>();  
            Random random = new Random();
            for(int i = 0;i<100000;i++){
                int age = random.nextInt(100);
                userList.add(new User(age,i));
            }
            List<Cumster> cList = new ArrayList<Cumster>();
            for(int i = 0;i<100000;i++){
                cList.add(new Cumster(RandomDemo.getRandom(10,RandomDemo.TYPE.LETTER_CAPITAL),i));
            }
            //使用普通for循环遍历
    //        long start = System.currentTimeMillis();
    //        for (int i = 0; i < cList.size(); i ++) {
    //            User user = userList.get(i);
    //            for(int j=0;j<userList.size();j++){
    //                Cumster cumster = cList.get(j);
    //                if(user.getId() == cumster.getId()){
    //                    user.setName(cumster.getName());
    //                }
    //            }
    //        }
    //        long end = System.currentTimeMillis();
    
            //使用for-each迭代循环遍历
            List<User> strList = new ArrayList<User>();
            long start = System.currentTimeMillis();
            for (Cumster cumster : cList) {
                for (User user : userList) {
                    if(cumster.getId() == user.getId()){
                        user.setName(cumster.getName());
                        strList.add(user);
                    }
                }
            }
            long end = System.currentTimeMillis();
            
            for (int i = 0; i < userList.size(); i ++) {
                System.out.print(userList.get(i).getName()+",");
            }
    //        Collections.sort(userList, Comparator.comparing(User::getId));
            Collections.sort(userList, new Comparator<User>(){
                /*
                 * int compare(RateInfo R1, RateInfo R2) 返回一个基本类型的整型,
                 * 返回负数表示:R1 小于R2,
                 * 返回0 表示:R1和R2相等,
                 * 返回正数表示:R1大于R2
                 */
                public int compare(User R1, User R2) {
                    //按照RateCode的年龄进行升序排列
                    if(R1.getId() > R2.getId()){
                        return 1;
                    }
                    if(R1.getId() == R2.getId()){
                        return 0;
                    }
                    return -1;
                }
            });
            System.out.println();
            for (User user : userList) {
                System.out.println(user.getId()+":"+user.getMany()+","+user.getName());
            }
            System.out.println(strList.toString());
            System.out.println("运行时间:"+(end-start));
        }
    }

    结果:

      1.使用for-each循环10万个数据进行循环运行时间:

      

      2.使用普通for循环10万个数据进行循环运行时间:

      

  • 相关阅读:
    Django:同一个app支持多个数据库
    Django部署以及整合celery
    软件破解码等
    操作日志的设计小结by大熊
    用户-权限-组织-三员分立
    mysql调优
    笔试面试中常见的位运算用法
    Ubuntu系统下搭建Java平台
    All about Oracle Character Set
    各位技术大牛们的逆袭集锦!屌丝们都看过来!
  • 原文地址:https://www.cnblogs.com/x-ll123/p/9317258.html
Copyright © 2011-2022 走看看