list<br>Listへ<br>へんかん
2666 ワード
package com.edwin.common;
import java.lang.reflect.Method;
import java.util.List;
public class RunTime {
public static long invokeStaticMethod(String clsName, String methodName,
Object[] args) throws Exception {
long start = System.nanoTime();
try {
Class c = Class.forName(clsName);
Class[] argsClass = new Class[] {List.class};
Method method = c.getMethod(methodName, argsClass);
method.invoke(c, args);
} catch (Exception e) {
e.printStackTrace();
}
long end = System.nanoTime();
return end - start;
}
}
package com.edwin.common;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Transformer;
public class Test {
/**
* @param args
*/
public static List<Integer> StringToIntegerLst(List<String> inList){
List<Integer> iList =new ArrayList<Integer>(inList.size());
try{
for(int i=0,j=inList.size();i<j;i++){
iList.add(Integer.parseInt(inList.get(i)));
}
}catch(Exception e){
}
return iList;
}
public static List<Integer> CollStringToIntegerLst(List<String> inList){
List<Integer> iList =new ArrayList<Integer>(inList.size());
CollectionUtils.collect(inList,
new Transformer(){
public java.lang.Object transform(java.lang.Object input){
return new Integer((String)input);
}
} ,iList );
return iList;
}
public static void main(String[] args) {
List<String> sList = new ArrayList<String>();
for (int i=0;i<1000;i++) {
sList.add(String.valueOf(i));
}
Object[] param=new Object[]{sList};
try {
long runTime=RunTime.invokeStaticMethod("com.edwin.common.Test", "StringToIntegerLst", param);
System.out.println(" "+runTime);
long runTimeByColl=RunTime.invokeStaticMethod("com.edwin.common.Test", "CollStringToIntegerLst", param);
System.out.println(" org.apache.commons.collections.CollectionUtils "+runTimeByColl);
System.out.println(" (runTimeByColl-runTime)=" +String.valueOf(runTimeByColl-runTime));
} catch (Exception e) {
e.printStackTrace();
}
}
}