list<String> 向List<Integer>转化!!谁有好方法!!
看了一下org.apache.commons.collections 
Interface Transformer 接口提供的让人郁闷!可能是我不会用!顺便给看看这个帖子:http://topic.csdn.net/u/20090610/11/25285590-9fae-4de4-8eec-f6c0f4713806.html

解决方案 »

  1.   

    楼主还是自己写个Util的方法吧:
    for(String str : list) {
      int i = Integer.paseInt(str);
      intList.add(i);
    }
      

  2.   

    import java.util.ArrayList;
    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);
    }
    }
    }
      

  3.   

    各位我就是那么写的呀!!这不要更好的方法么!
    原来的版本:
    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;
    }
      

  4.   

    我觉得把 try 放在for 里面好点 ..
      

  5.   

    catch到exception应该做处理啊。不然程序退出,你都不知道发生了什么事。
      

  6.   

    commons包的功能还是很强大的,最好还是用commons吧,别自己转,会考虑的不全面的。
      

  7.   

    试一下这个代码:List <String>  inputCollection  = ... ;
    List <Integer> outputCollection = ... ;
    CollectionUtils.collect(inputCollection, 
          new Transformer(){
            public java.lang.Object transform(java.lang.Object input){
              return new Integer((String)input);
            }
          } ,outputCollection );
      

  8.   

    刚开初也想用!!也看过这个接口,其一是返回Object ,不支持泛型!!其次是
    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鱼和熊掌的关系!!!
      

  9.   

    测试结果出炉了!!大家快看!!!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();
    }
    }
    }
      

  10.   

    结果:
    采用顺序转化方法执行时间9843710
    采用org.apache.commons.collections.CollectionUtils执行时间14854681
    微秒相差(runTimeByColl-runTime)=5010971不是很悬殊!!!
      

  11.   

    list <Object>list <?>list <T>