1、LinkedList与ArrayList的区别
1)、LinkedList是基于链表的数据结构,ArrayList是实现了基于动态数组的数据结构;
2)、对于查询(get),ArrayList比LinkedList效率高,因为LinkedList要移动指针
对于新增(insert)和删除(remove),LinkedList比ArrayList效率高,因为ArrayList要移动数据
代码如下:
public class ComporeTest { private static Logger logger = LoggerFactory.getLogger(ComporeTest.class); private static Integer N = 5000; public static Long timeList(List list){ Long start = System.currentTimeMillis(); logger.info("start{}",start); for(int i = 0; i < N; i++){ list.add(i); } logger.info("timeList{}",System.currentTimeMillis() - start); return System.currentTimeMillis() - start; } public static List addList(List list){ Object o = new Object(); for(int i = 0;i < N; i++){ list.add(0,o); } logger.info("测试ArrayList与LinkedList的效率"); return list; } static Long readList(List list){ long start = System.currentTimeMillis(); for(int i = 0; i < list.size(); i++){ list.get(i); } return System.currentTimeMillis() - start ; } public static void main(String [] args){ logger.info("ArrayList添加"+N+"条消耗时间:{}",timeList(new ArrayList())); logger.info("LinkedList添加"+N+"条消耗时间:{}",timeList(new LinkedList())); List arrayList = addList(new ArrayList<>()) ; List linkedList = addList(new LinkedList<>()); logger.info("ArrayList查询"+N+"消耗时间:{}",readList(arrayList)); logger.info("LinkedList查询"+N+"消耗时间:{}",readList(linkedList)); }}测试结果:
原文地址:https://www.cnblogs.com/smileZ/p/9805353.html
时间: 2024-10-02 10:11:42