美文网首页
ArrayList与LinkedList的区别

ArrayList与LinkedList的区别

作者: 浮安樊 | 来源:发表于2018-06-11 02:19 被阅读0次

    DZone

    class hierarchy diagram of Collection

    ArrayList vs. LinkedList vs. Vector

    From the hierarchy diagram, they all implement List interface. They are very similar to use. Their main difference is their implementation which causes different performance for different operations.

    ArrayList is implemented as a resizable array. As more elements are added to ArrayList, its size is increased dynamically. It's elements can be accessed directly by using the get and set methods, since ArrayList is essentially an array.

    LinkedList is implemented as a double linked list. Its performance on add and remove is better than ArrayList, but worse on get and set methods.

    Vector is similar with ArrayList, but it is synchronized (Thread-safe).

    ArrayList is a better choice if your program is thread-safe. Vector and ArrayList require space as more elements are added. Vector each time doubles its array size, while ArrayList grow 50% of its size each time. LinkedList, however, also implements Queue interface which adds more methods than ArrayList and Vector, such as offer(), peek(), poll(), etc.

    Note: The default initial capacity of an ArrayList is pretty small. It is a good habit to construct the ArrayList with a higher initial capacity. This can avoid the resizing cost.

    Performance of ArrayList vs. LinkedList

    相关文章

      网友评论

          本文标题:ArrayList与LinkedList的区别

          本文链接:https://www.haomeiwen.com/subject/fchceftx.html