• 解析ArrayList与LinkedList的遍历方法


    ArrayList与LinkedList都是实现List接口,前者底层是数据存储,而LinkedList则是以链表的形式存储,因此两个都各有其独自的特性。在两者中,遍历方法也随着改变而有所不同。

    遍历方法

    通用的List的遍历方法有三种:

    1、for get循环遍历

    2、foreach遍历

    3、for Iterator迭代遍历

    遍历结果

    arrayList length: 360000

    linkedList length: 360000

    First: for get

    arrayList need time: 15ms

    linkedList need time: 239653ms

    Second: foreach

    arrayList need time: 22ms

    linkedList need time: 20ms

    Thrid: foreach itertor

    arrayList need time: 17 ms

    linkedList need time: 8 ms

    这是在三十六万条数据下的结果。可以得出一个初步结论,linkedList使用get方法的效率极低,而linkedList使用foreach和itertor的表现是优于arraylist的。如果再把数据扩大十倍,让数据达到三百六十万条数据,在该情况下的速度结构更加体现了两者在不同遍历方法下的区别。

    源码分析

    ArrayList get:

    public E get(int index) {
        rangeCheck(index); //越界检查
    
        return elementData(index);
    }
    

    因为ArrayList存储为数组存储,所以可以直接用下表来访问,速度极快。

    LinkedList get:

    public E get(int index) {
        checkElementIndex(index);
        return node(index).item;
    }
    Node<E> node(int index) {
        // assert isElementIndex(index);
    
        if (index < (size >> 1)) {
            Node<E> x = first;
            for (int i = 0; i < index; i++)
                x = x.next;
            return x;
        } else {
            Node<E> x = last;
            for (int i = size - 1; i > index; i--)
                x = x.prev;
            return x;
        }
    }
    

    LinkedList存储为Node,也就是双向链表方式存储,虽然两头都可以进行遍历,但是每次都要重复进行从头遍历元素,效率自然就慢了。

    而foreach和迭代器方式两者的区别不大。

    总结

    1、ArrayList使用Get来遍历元素;Linkedlist多用foreach或迭代器方式遍历。

    2、java开发当中为了能够尽可能的挖掘优化的点,应当对代码中的结构有深刻的理解,了解其中的构造,那么自然能够优化出速度快优美的代码。

    附:遍历代码

     public static void main(String[] args) throws Exception{
        ArrayList<String> arrayList = new ArrayList<String>();
        LinkedList<String> linkedList = new LinkedList<String>();
        int num = 360000;
    
        Random random = new Random();
        String randomStr = "";
        for (int i = 0; i < num; i++){
            randomStr = String.valueOf(random.nextFloat());
            arrayList.add(randomStr);
            linkedList.add(randomStr);
        }
    
        System.out.println("------------------");
        System.out.println("arrayList length: "+arrayList.size());
        System.out.println("linkedList length: "+linkedList.size());
        System.out.println("------------------");
    
        System.out.println("------The way------");
        System.out.println("First: for get");
        long time1 = System.currentTimeMillis();
        for (int i =0;i<arrayList.size();i++) {
            String temp = arrayList.get(i);
        }
        long time2 = System.currentTimeMillis();
        System.out.println("arrayList need time: "+(time2-time1)+"ms");
        long time11 = System.currentTimeMillis();
        for (int i =0;i<linkedList.size();i++) {
            String temp = linkedList.get(i);
        }
        long time22 = System.currentTimeMillis();
        System.out.println("linkedList need time: "+(time22-time11)+"ms");
    
        System.out.println("
    Second: foreach");
        long time3 = System.currentTimeMillis();
        for (String temp:arrayList) {
            String t = new String();
        }
        long time4 = System.currentTimeMillis();
        System.out.println("arrayList need time: "+(time4-time3)+"ms");
        long time33 = System.currentTimeMillis();
        for (String temp:linkedList) {
            String t = new String();
        }
        long time44 = System.currentTimeMillis();
        System.out.println("linkedList  need time: "+(time44-time33)+"ms");
    
        System.out.println("
    Thrid: foreach itertor");
        long time5 = System.currentTimeMillis();
        Iterator<String> iterator1 = arrayList.iterator();
        while ( iterator1.hasNext()) {
            String t = iterator1.next();
        }
        long time6 = System.currentTimeMillis();
        System.out.println("arrayList need time: "+(time6-time5)+" ms");
        long time55 = System.currentTimeMillis();
        Iterator<String> iterator2 = linkedList.iterator();
        while ( iterator2.hasNext()) {
            String t = iterator2.next();
        }
        long time66 = System.currentTimeMillis();
        System.out.println("linkedList need time: "+(time66-time55)+" ms");
    }//main
  • 既然不能成为屠龙的勇士,那么就好好成为一名优秀的管家,为公主建设一个温馨美好的家。
    Since it can not become a dragon warrior, then it is a good housekeeper, for the princess to build a warm and beautiful home.

  • 相关阅读:
    html$css_day05
    html$css_day04
    html$css_day03
    html$css_day02
    html$css_day01
    日常笔记19/3/04-19/3/10
    堆排序
    js对象之XMLHttpReques对象学习
    前端页面显示问题解决步骤(方法)
    SpringBoot跨域小结
  • 原文地址:https://www.cnblogs.com/ITflying/p/7261764.html
  • Copyright © 2020-2023  润新知