ArrayList的方法:remove(object o) 、remove(int index)、removeAll(Collection c)时间复杂度的比较
来源:互联网 发布:数据库实用教程pdf 编辑:程序博客网 时间:2024/06/13 05:35
1、首先remove(int index)肯定比remove(object o)快,索引肯定快啦,毫无疑问,当然知道index的前提下
2、假如:有List_1集合140000条数据,List_2集合70000条
代码1:
打印结果:
size: 14000
time: 44ms
size: 7000
如果数量乘以10,也就是14000*10
140000
time: 4229ms
size: 70000
说明:时间消耗差不多10*10倍,因为remove(object o)内部还有1个循环,加上外面对list2的foreach,有2个循环,
说明时间复杂度是O(n^2),
代码2:
打印结果:
size: 14000
time: 91ms
size: 7000
如果数量乘以10,也就是14000*10
size: 140000
time: 8820ms
size: 70000
说明:时间消耗差不多10*10倍,因为removeAll()嵌套了2个for循环,list1的循环和list2的,说明时间复杂度也是O(n^2)
代码1和代码2的总结:都是从list1移除list2,代码1比代码2的花费的时间明显少,
也就是说remove(object o)比removeAll()效率更高
2、假如:有List_1集合140000条数据,List_2集合70000条
代码1:
List<Integer> list1 = new ArrayList<>();List<Integer> list2 = new ArrayList<>();for(int i = 0;i<14000;i++) {list1.add(i);if (i%2==0) {list2.add(i);}}System.out.println("size: "+list1.size());long start = System.currentTimeMillis();for (Integer i2 : list2) {list1.remove(i2);//此处有个小问题:remove(i2)的参数类型是(object o)还是(int index);如果是这样呢:remove(2),参数又是什么类型。经过测试:前者是object,后者是int。这算是意料之中吧}long end = System.currentTimeMillis();System.out.println("time: "+(end-start)+"ms\n"+"size: "+list1.size());
打印结果:
size: 14000
time: 44ms
size: 7000
如果数量乘以10,也就是14000*10
140000
time: 4229ms
size: 70000
说明:时间消耗差不多10*10倍,因为remove(object o)内部还有1个循环,加上外面对list2的foreach,有2个循环,
说明时间复杂度是O(n^2),
代码2:
List<Integer> list1 = new ArrayList<>();List<Integer> list2 = new ArrayList<>();for(int i = 0;i<14000;i++) {list1.add(i);if (i%2==0) {list2.add(i);}}System.out.println(list1.size());long start = System.currentTimeMillis();list1.removeAll(list2);long end = System.currentTimeMillis();System.out.println("time: "+(end-start)+"ms\n"+"size: "+list1.size());
打印结果:
size: 14000
time: 91ms
size: 7000
如果数量乘以10,也就是14000*10
size: 140000
time: 8820ms
size: 70000
说明:时间消耗差不多10*10倍,因为removeAll()嵌套了2个for循环,list1的循环和list2的,说明时间复杂度也是O(n^2)
代码1和代码2的总结:都是从list1移除list2,代码1比代码2的花费的时间明显少,
也就是说remove(object o)比removeAll()效率更高
阅读全文
0 0
- ArrayList的方法:remove(object o) 、remove(int index)、removeAll(Collection c)时间复杂度的比较
- Java中ArrayList 的remove&removeall
- ArrayList的remove()方法
- ArrayList的add(Object obj)和remove(Object obj)和remove(index)和get(index)的源码分析
- collection的remove方法对HashSet ArrayList的不同
- 细说ArrayList的remove方法
- ArrayList、Iterator的remove方法
- ArrayList和LinkedList add和remove方法的比较
- JDK源码-Collection-remove()以及removeAll()
- Collection 和 Iteractor 的remove方法
- Collection和List接口的remove方法
- java collection Iterator的remove()方法
- ArrayList的remove方法的一个特性
- 详解ArrayList的remove的方法
- 关于ArrayList的remove
- Iterator的remove()和Collection的remove()
- Iterator的remove()和Collection的remove()
- Iterator的remove()和Collection的remove()
- mybatis注册映射文件
- 交叉验证
- 使用JMeter创建数据库(Mysql)测试
- 【递归练习】算24点
- 修改WampServer的默认端口
- ArrayList的方法:remove(object o) 、remove(int index)、removeAll(Collection c)时间复杂度的比较
- K倍区间
- ROS Navigation的costmap_2d类继承关系与实现方法
- 2016级数据结构结课总结
- Linux-内存-数据结构
- RTC使用野火例程,LSE复位可用,HSE和LSI不可用问题。
- TensorFlow实战——个性化推荐
- 两个数比大小遇到的问题
- 写给自己的JAVA工程师之路-多线程