list<String> 向List<Integer>转化!!谁有好方法!!
看了一下org.apache.commons.collections
Interface Transformer 接口提供的让人郁闷!可能是我不会用!顺便给看看这个帖子:http://topic.csdn.net/u/20090610/11/25285590-9fae-4de4-8eec-f6c0f4713806.html
看了一下org.apache.commons.collections
Interface Transformer 接口提供的让人郁闷!可能是我不会用!顺便给看看这个帖子:http://topic.csdn.net/u/20090610/11/25285590-9fae-4de4-8eec-f6c0f4713806.html
for(String str : list) {
int i = Integer.paseInt(str);
intList.add(i);
}
import java.util.List;
public class StrToInt {
public static void main(String[] args) {
List<String> lstr = new ArrayList<String>();
lstr.add("23");
lstr.add("g42");
lstr.add("2");
lstr.add("er");
lstr.add("ere");
List<Integer> lint = new ArrayList<Integer>();
for(String str:lstr){
if(!str.matches("^([0-9])+$")){
continue;
}
int i = Integer.parseInt(str);
lint.add(i);
}
for(int i:lint){
System.out.println(i);
}
}
}
原来的版本:
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;
}
List <Integer> outputCollection = ... ;
CollectionUtils.collect(inputCollection,
new Transformer(){
public java.lang.Object transform(java.lang.Object input){
return new Integer((String)input);
}
} ,outputCollection );
inputCollection - the collection to get the input from, may be null
transformer - the transformer to use, may be null
outputCollection - the collection to output into, may not be null鱼和熊掌的关系!!!
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();
}
}
}
采用顺序转化方法执行时间9843710
采用org.apache.commons.collections.CollectionUtils执行时间14854681
微秒相差(runTimeByColl-runTime)=5010971不是很悬殊!!!