zoukankan      html  css  js  c++  java
  • Java下List<Long>转List<String>或者List<Long>转List<Integer>

    说明:很遗憾,没有快速方法,只能遍历然后循环增加进去。

    方法:

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

    如果借助第三方类库可以这样实现:

    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();
            }
        }
    }

    参考:

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

  • 相关阅读:
    读书-《智能时代》-机器智能正在革我们的命
    判断Http服务器是否支持支持断点续传
    最全Html标签Meta介绍
    用PHP整理照片和视频文件
    读书-《癌症.真相:医生也在读》-我所认识的癌症
    scrapy-redis组件配置用例
    Scrapy+seleninu抓取内容同时下载图片几个问题
    无界浏览器Chorme命令行开关
    Scrapy Crawl 运行出错 AttributeError: 'xxxSpider' object has no attribute '_rules' 的问题解决
    福利,OpenCV最新中文版官方教程来了
  • 原文地址:https://www.cnblogs.com/EasonJim/p/8258118.html
Copyright © 2011-2022 走看看