• 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

  • 相关阅读:
    从Java角度理解Angular之入门篇:npm, yarn, Angular CLI
    大数据开发实战:Stream SQL实时开发一
    大数据开发实战:Spark Streaming流计算开发
    大数据开发实战:Storm流计算开发
    大数据开发实战:Hadoop数据仓库开发实战
    数据仓库中的拉链表
    java通过jdbc连接impala
    impala-shell常用命令
    Kudu-java数据库简单操作
    拉链表--实现、更新及回滚的具体实现( 转载)
  • 原文地址:https://www.cnblogs.com/EasonJim/p/8258118.html
Copyright © 2020-2023  润新知