对两个LIST对行比对,筛选
以下两种方法比对中会发现用情况 (单位 ns)
CollectionUtils ListUtils
52354 811485
49659 790698
package com.collection;
import java.util.*;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
public class ListFilter {
public static void main(String[] args) {
List l1 = new ArrayList();
List l2 = new ArrayList();
l1.add(1);
l1.add(2);
l1.add(3);
l1.add(5);
l2.add(4);
l2.add(5);
Collection s1 = CollectionUtils.union(l1, l2);
Long start = System.nanoTime();
Collection s2 = CollectionUtils.subtract(l1, l2);
System.out.println(System.nanoTime() - start);
Long start1 = System.nanoTime();
List ls = ListUtils.subtract(l1, l2);
System.out.println(System.nanoTime() - start1);
Collection s3 = CollectionUtils.intersection(l1, l2);
Collection s4 = CollectionUtils.disjunction(l1, l2);
System.out.println(s4);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
本文对比了Apache Commons Collections的CollectionUtils和ListUtils库在执行集合操作时的时间性能,包括集合合并、减法、交集和并集操作。通过使用NanoTime进行精确计时,展示了在不同情况下这两种工具的效率差异。
296

被折叠的 条评论
为什么被折叠?



