Java下List 轉List 或者List 轉List

說明:很遺憾,沒有快速方法,只能遍歷而後循環增長進去。java

方法:apache

for(String str : list) { int i = Integer.paseInt(str); intList.add(i); } 

若是藉助第三方類庫能夠這樣實現:spa

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; } }
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.jsoft.common.Test", "StringToIntegerLst", param); System.out.println("採用順序轉化方法執行時間"+runTime); long runTimeByColl=RunTime.invokeStaticMethod("com.jsoft.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(); } } }

 

參考:.net

http://bbs.csdn.net/topics/310077387code

相關文章
相關標籤/搜索