Saturday, 20 January 2018

CHEAT SHEET for java List interface, LinkedList and ArrayList

Consider Below points before choosing List concrete implementations for your use case: 

  •    ArrayList is a linear data structure and  re-sizable/dynamic array
  •    Initial size can be given. and if not given the default size is 10
  •    It is always good practice to give initial size and advised not to give
             very large number as size
  •   Good programmers prefer ArrayList to LinkedList if retrieval operations is
           done more frequently than insertion and deletions of elements.
  •    Better choose LinkedList if insertion and deletion operations are done
             frequently compared to retrieval operations.
  •    Lists are not sorted. if required use Collections.sort() to sort
  •    Collections.sort() uses optimized merge sort [tim sort] on ArrayList
  •    However legacy merge sort also can be used using a switch. But it is going to be deprecated  in near future 

  •  The major difference between ArrayList and LinkedList is RandomAccess behavior 

  •  ArrayLists uses Random access indexing where as LinkedLists uses
          Sequential access indexing
  •   LinkedList carries extra overhead in the form of reference to the next and
            previous elements other than element's data itself

  •   LinkedList uses Doubly LinkedList data structure internally

  •   Deque is the java DoubleLinkedList data structure implementation

  •   Before choosing List implementation ask yourself below questions 
                    a. Are you aware of initial size? 
                    b. Which operations are used more frequently? [insertion, deletion,
                       retrieval, search, sort are few among others]
  •   Search Algorithm: 
                  a. Collections class uses binary search if List is ArrayList
                  b. Collections class uses linear search if List is of type LinkedList

  •  ArrayList and LinkedList both uses fast fail iterations.

  •   If another thread tries to modify list while iterating, java runtime throws  ConcurrentModificationException. This can be considered as fast fail behavior  

No comments:

Post a Comment

Java and OOPS concepts

Java is an object oriented programming language which can be downloaded and installed in your local Machine from To explain jav...