When should I use a List vs a LinkedList

后端 未结 15 2116
挽巷
挽巷 2020-11-22 14:50

When is it better to use a List vs a LinkedList?

相关标签:
15条回答
  • 2020-11-22 15:08

    The difference between List and LinkedList lies in their underlying implementation. List is array based collection (ArrayList). LinkedList is node-pointer based collection (LinkedListNode). On the API level usage, both of them are pretty much the same since both implement same set of interfaces such as ICollection, IEnumerable, etc.

    The key difference comes when performance matter. For example, if you are implementing the list that has heavy "INSERT" operation, LinkedList outperforms List. Since LinkedList can do it in O(1) time, but List may need to expand the size of underlying array. For more information/detail you might want to read up on the algorithmic difference between LinkedList and array data structures. http://en.wikipedia.org/wiki/Linked_list and Array

    Hope this help,

    0 讨论(0)
  • 2020-11-22 15:10

    Linked lists provide very fast insertion or deletion of a list member. Each member in a linked list contains a pointer to the next member in the list so to insert a member at position i:

    • update the pointer in member i-1 to point to the new member
    • set the pointer in the new member to point to member i

    The disadvantage to a linked list is that random access is not possible. Accessing a member requires traversing the list until the desired member is found.

    0 讨论(0)
  • 2020-11-22 15:11

    Use LinkedList<> when

    1. You don't know how many objects are coming through the flood gate. For example, Token Stream.
    2. When you ONLY wanted to delete\insert at the ends.

    For everything else, it is better to use List<>.

    0 讨论(0)
  • 2020-11-22 15:14

    The primary advantage of linked lists over arrays is that the links provide us with the capability to rearrange the items efficiently. Sedgewick, p. 91

    0 讨论(0)
  • 2020-11-22 15:23

    Thinking of a linked list as a list can be a bit misleading. It's more like a chain. In fact, in .NET, LinkedList<T> does not even implement IList<T>. There is no real concept of index in a linked list, even though it may seem there is. Certainly none of the methods provided on the class accept indexes.

    Linked lists may be singly linked, or doubly linked. This refers to whether each element in the chain has a link only to the next one (singly linked) or to both the prior/next elements (doubly linked). LinkedList<T> is doubly linked.

    Internally, List<T> is backed by an array. This provides a very compact representation in memory. Conversely, LinkedList<T> involves additional memory to store the bidirectional links between successive elements. So the memory footprint of a LinkedList<T> will generally be larger than for List<T> (with the caveat that List<T> can have unused internal array elements to improve performance during append operations.)

    They have different performance characteristics too:

    Append

    • LinkedList<T>.AddLast(item) constant time
    • List<T>.Add(item) amortized constant time, linear worst case

    Prepend

    • LinkedList<T>.AddFirst(item) constant time
    • List<T>.Insert(0, item) linear time

    Insertion

    • LinkedList<T>.AddBefore(node, item) constant time
    • LinkedList<T>.AddAfter(node, item) constant time
    • List<T>.Insert(index, item) linear time

    Removal

    • LinkedList<T>.Remove(item) linear time
    • LinkedList<T>.Remove(node) constant time
    • List<T>.Remove(item) linear time
    • List<T>.RemoveAt(index) linear time

    Count

    • LinkedList<T>.Count constant time
    • List<T>.Count constant time

    Contains

    • LinkedList<T>.Contains(item) linear time
    • List<T>.Contains(item) linear time

    Clear

    • LinkedList<T>.Clear() linear time
    • List<T>.Clear() linear time

    As you can see, they're mostly equivalent. In practice, the API of LinkedList<T> is more cumbersome to use, and details of its internal needs spill out into your code.

    However, if you need to do many insertions/removals from within a list, it offers constant time. List<T> offers linear time, as extra items in the list must be shuffled around after the insertion/removal.

    0 讨论(0)
  • 2020-11-22 15:23

    A common circumstance to use LinkedList is like this:

    Suppose you want to remove many certain strings from a list of strings with a large size, say 100,000. The strings to remove can be looked up in HashSet dic, and the list of strings is believed to contain between 30,000 to 60,000 such strings to remove.

    Then what's the best type of List for storing the 100,000 Strings? The answer is LinkedList. If the they are stored in an ArrayList, then iterating over it and removing matched Strings whould take up to billions of operations, while it takes just around 100,000 operations by using an iterator and the remove() method.

    LinkedList<String> strings = readStrings();
    HashSet<String> dic = readDic();
    Iterator<String> iterator = strings.iterator();
    while (iterator.hasNext()){
        String string = iterator.next();
        if (dic.contains(string))
        iterator.remove();
    }
    
    0 讨论(0)
提交回复
热议问题