list 向List转化!!谁有好方法!!

APOLLO_TS 2009-06-12 11:44:00
list<String> 向List<Integer>转化!!谁有好方法!!
看了一下org.apache.commons.collections
Interface Transformer 接口提供的让人郁闷!可能是我不会用!

顺便给看看这个帖子:http://topic.csdn.net/u/20090610/11/25285590-9fae-4de4-8eec-f6c0f4713806.html
...全文
7247 22 打赏 收藏 转发到动态 举报
写回复
用AI写文章
22 条回复
切换为时间正序
请发表友善的回复…
发表回复
johnston678 2009-06-15
  • 打赏
  • 举报
回复 1
如果有现成最好用现成的,
自己写出来的应该没有现在产品好!
kbyst 2009-06-15
  • 打赏
  • 举报
回复
list <Object>

list <?>

list <T>
yuzhonglian2004 2009-06-15
  • 打赏
  • 举报
回复
差距还是蛮大的。。。
geminimyth 2009-06-15
  • 打赏
  • 举报
回复
学习中
老紫竹 2009-06-15
  • 打赏
  • 举报
回复
无它,一个一个转好了!
APOLLO_TS 2009-06-15
  • 打赏
  • 举报
回复
结果:
采用顺序转化方法执行时间9843710
采用org.apache.commons.collections.CollectionUtils执行时间14854681
微秒相差(runTimeByColl-runTime)=5010971

不是很悬殊!!!
APOLLO_TS 2009-06-15
  • 打赏
  • 举报
回复
测试结果出炉了!!大家快看!!!

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();
}
}
}
APOLLO_TS 2009-06-15
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 preferme 的回复:]
试一下这个代码:
Java code

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


[/Quote]
刚开初也想用!!也看过这个接口,其一是返回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

鱼和熊掌的关系!!!


yztommyhc 2009-06-13
  • 打赏
  • 举报
回复
commons包的功能还是很强大的,最好还是用commons吧,别自己转,会考虑的不全面的。
bigbug9002 2009-06-13
  • 打赏
  • 举报
回复
catch到exception应该做处理啊。不然程序退出,你都不知道发生了什么事。
cantalou 2009-06-13
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 laorer 的回复:]
我觉得把 try 放在for 里面好点 ..
[/Quote]


能不能解释下
shanchailee 2009-06-13
  • 打赏
  • 举报
回复
正解[Quote=引用 13 楼 preferme 的回复:]
试一下这个代码:

Java code
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 );
[/Quote]
冰思雨 2009-06-13
  • 打赏
  • 举报
回复
试一下这个代码:

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 );
APOLLO_TS 2009-06-13
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 yztommyhc 的回复:]
commons包的功能还是很强大的,最好还是用commons吧,别自己转,会考虑的不全面的。
[/Quote]
你给写一个吧!!还真不知道怎么整!!
APOLLO_TS 2009-06-12
  • 打赏
  • 举报
回复
各位我就是那么写的呀!!这不要更好的方法么!
原来的版本:
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;
}

Qstr_run 2009-06-12
  • 打赏
  • 举报
回复
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);
}
}
}
knightzhuwei 2009-06-12
  • 打赏
  • 举报
回复
就算有现成的方法 肯定也是一个一个转的啊。。难道有比一个一个转代价更小的办法么?
laorer 2009-06-12
  • 打赏
  • 举报
回复
是的,还是一个个的转吧,就一个函数而矣

nujiah001 2009-06-12
  • 打赏
  • 举报
回复
up
xwmr1988 2009-06-12
  • 打赏
  • 举报
回复
加载更多回复(2)

62,636

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧