在将集合拆分/加入到数组或迭代集合本身之间的速度更快

时间:2012-04-25 22:30:58

标签: java

我有两个代码tbat做同样的事情。我想知道哪一个会更快:

1

import org.apache.commons.collections.CollectionUtils;
String [] htArray = StringUtils.join (CollectionUtils.subtract (
    Arrays.asList ((h + " " + t).split (" ") ),
    Arrays.asList (htSelected.split (" ") ) ), " " ).split (" ");
for (String term: htArray ) {
    ...
}

2。

import org.apache.commons.collections.CollectionUtils;
ArrayList <String> htList = null;
try {
    htList = (ArrayList <String>) CollectionUtils.subtract (
        Arrays.asList ( (h + " " + t).split (" ") ), 
        Arrays.asList (htSelected.split (" ") ) );
} catch (Exception except) {}

if ( htList != null) {
    for (String term: htList) {
        ...
    }
}

第一个加入一个集合然后将字符串拆分成一个数组。第二个投射集合,尝试/捕获然后添加'如果'。哪一个是最优的?

1 个答案:

答案 0 :(得分:1)

运行100次并找出