日常工作 经常需要取两个数据集的交集。对常用的List 和Set集合做了一个测试
public static void main(String[] args) {List<Integer> list1 = Lists.newArrayList();List<Integer> list2 = Lists.newArrayList();Set<Integer> set3 = Sets.newHashSet();Set<Integer> set4 = Sets.newHashSet();for (int i = 0; i < 100000; i++) {list1.add(i);set3.add(i);list2.add(i);set4.add(i);}System.out.println("list1 size " + list1.size());System.out.println("list2 size " + list2.size());System.out.println("set3 size " + set3.size());System.out.println("set4 size " + set4.size());StopWatch stopWatch =new StopWatch("交集测试");stopWatch.start("list交集测试");list2.parallelStream().filter(data -> list1.contains(data)).collect(Collectors.toList());stopWatch.stop();stopWatch.start("set交集测试");set4.parallelStream().filter(data -> set3.contains(data)).collect(Collectors.toSet());stopWatch.stop();StopWatch.TaskInfo[] taskInfo = stopWatch.getTaskInfo();Arrays.stream(taskInfo).forEach(taskInfo1 -> {System.out.println(taskInfo1.getTaskName()+"---"+taskInfo1.getTimeMillis()+"毫秒");});}
测试结果如下:数据量都是10万的情况下。set的性能是 164.6倍多。