public void test(List<RecordLoan> recordLoanList){
        for (int i = 0; i < recordLoanList.size(); i++) {
            Map map = new HashMap();
            map.put("1",recordLoanList.get(i).getGid());
            map.put("2",recordLoanList.get(i).getGid());
            map.put("3",recordLoanList.get(i).getGid());
            map.put("4",recordLoanList.get(i).getGid());
            map.put("5",recordLoanList.get(i).getGid());
        }
    }public void test(List<RecordLoan> recordLoanList){
        for (int i = 0; i < recordLoanList.size(); i++) {
            RecordLoan recordLoan = recordLoanList.get(i);
            Map map = new HashMap();
            map.put("1",recordLoan.getGid());
            map.put("2",recordLoan.getGid());
            map.put("3",recordLoan.getGid());
            map.put("4",recordLoan.getGid());
            map.put("5",recordLoan.getGid());
        }
    }这两种哪个效率高 性能好

解决方案 »

  1.   

    第二种效率高,
    第一种每次执行一个循环比第二种多调用四次 recordLoanList.get(i)
    public void test(List<RecordLoan> recordLoanList){
            for (int i = 0; i < recordLoanList.size(); i++) {
                RecordLoan recordLoan = recordLoanList.get(i);
                Map map = new HashMap();
                map.put("1",recordLoan.getGid());
                map.put("2",recordLoan.getGid());
                map.put("3",recordLoan.getGid());
                map.put("4",recordLoan.getGid());
                map.put("5",recordLoan.getGid());
            }
        }
    这个方法如果被程序中的其他方法调用,没有任何意思,因为没有返回值也没有改变传参的值,也没有改变成员变量的值
      

  2.   

    第二种每次循环不是得创建recordLoan 新对象吗 这种是不是很好资源
      

  3.   

    第二种,
    第一中一个value值的产生,都生成过两个对象.2*4=8个对象.第二个则是1+4 =5个对象,一次循环少创建三个对象
      

  4.   

    哎,我刚没看清楚,map有五个key,一次循环是少创建4个对象
      

  5.   

    为什么不用forEach来做循环,效率应该会更好一点吧
      

  6.   

    第一种效率高,第二种循环中多了
     RecordLoan recordLoan = recordLoanList.get(i); 每次都要创建一个新对象。第一种会直接从recordLoanList.get(i).getGid() 复制一个放入map。第二种则是从recordLoan 中复制两种的开销差异主要是 多生成了一个  RecordLoan recordLoan
    recordLoanList.get(i) 带来的差异只是一两个CPU周期,基本可以忽略不计
      

  7.   

    直接用加强for循环不可以吗 是不是和第二种类似?
      

  8.   

    ArrayList 源码。看一看。效果一样    /**
         * Checks if the given index is in range.  If not, throws an appropriate
         * runtime exception.  This method does *not* check if the index is
         * negative: It is always used immediately prior to an array access,
         * which throws an ArrayIndexOutOfBoundsException if index is negative.
         */
        private void rangeCheck(int index) {
            if (index >= size)
                throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
        }    @SuppressWarnings("unchecked")
        E elementData(int index) {
            return (E) elementData[index];
        }    /**
         * Returns the element at the specified position in this list.
         *
         * @param  index index of the element to return
         * @return the element at the specified position in this list
         * @throws IndexOutOfBoundsException {@inheritDoc}
         */
        public E get(int index) {
            rangeCheck(index);        return elementData(index);
        }
      

  9.   

    ArrayList 源码。看一看。效果一样+1
      

  10.   

    为什么不直接用增强for循环?
    public void test(List<RecordLoan> recordLoanList){
            i=0;
           Map map = new HashMap();
            for (RecordLoan rl :recordLoanList) {               
                map.put("i++",rl .getGid());  
            }
        }
      

  11.   

    为什么不直接用增强for循环?
    public void test(List<RecordLoan> recordLoanList){
            i=0;
           Map map = new HashMap();
            for (RecordLoan rl :recordLoanList) {               
                map.put(i++ , rl .getGid());  
            }
        }
      

  12.   

    public void test(List<RecordLoan> recordLoanList){
            for (int i = 0; i < recordLoanList.size(); i++) {
                Object gid = recordLoanList.get(i).getGid();
                Map map = new HashMap();
                map.put("1",gid);
                map.put("2",gid);
                map.put("3",gid);
                map.put("4",gid);
                map.put("5",gid);
            }
        }
    按道理这样最快,因为一个循环里值查找了了次 gid这个对象。像你那样写的话每个循环里都会查5次,这个很好理解的
      

  13.   

    把创建对象方在外面,只创建一个对象,这样的话可以重复使用
    RecordLoan recordLoan =null;
    public void test(List<RecordLoan> recordLoanList){
            for (int i = 0; i < recordLoanList.size(); i++) {
               recordLoan  = recordLoanList.get(i);
                Map map = new HashMap();
                map.put("1",recordLoan.getGid());
                map.put("2",recordLoan.getGid());
                map.put("3",recordLoan.getGid());
                map.put("4",recordLoan.getGid());
                map.put("5",recordLoan.getGid());
            }
        }
      

  14.   

    个人认为第二种效率高,虽然没看源码,但是第一种map每次插入数据都要根据下标去取list对应下标的值,个人认为这个取值的过程是需要消耗性能的,数据量小可能看不出来,但是数据量大的数据就会体现出来。至于所谓的对象创建个数的问题,可以无视。