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());
}
}这两种哪个效率高 性能好
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());
}
}这两种哪个效率高 性能好
第一种每次执行一个循环比第二种多调用四次 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());
}
}
这个方法如果被程序中的其他方法调用,没有任何意思,因为没有返回值也没有改变传参的值,也没有改变成员变量的值
第一中一个value值的产生,都生成过两个对象.2*4=8个对象.第二个则是1+4 =5个对象,一次循环少创建三个对象
RecordLoan recordLoan = recordLoanList.get(i); 每次都要创建一个新对象。第一种会直接从recordLoanList.get(i).getGid() 复制一个放入map。第二种则是从recordLoan 中复制两种的开销差异主要是 多生成了一个 RecordLoan recordLoan
recordLoanList.get(i) 带来的差异只是一两个CPU周期,基本可以忽略不计
* 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);
}
public void test(List<RecordLoan> recordLoanList){
i=0;
Map map = new HashMap();
for (RecordLoan rl :recordLoanList) {
map.put("i++",rl .getGid());
}
}
public void test(List<RecordLoan> recordLoanList){
i=0;
Map map = new HashMap();
for (RecordLoan rl :recordLoanList) {
map.put(i++ , rl .getGid());
}
}
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次,这个很好理解的
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());
}
}