Upgrade to Pro — share decks privately, control downloads, hide ads and more …

collections in JDK

Kengo TODA
August 26, 2012

collections in JDK

Kengo TODA

August 26, 2012
Tweet

More Decks by Kengo TODA

Other Decks in Technology

Transcript

  1. ArrayList<T> It just wraps an array Good to #get(int): O(1)

    Bad to #remove(int), #contains(T) : O(n)
  2. LinkedList<T> Simple bidirectional list It costs more Java heap than

    ArrayList Good to #remove & #add for head/tail: O(1) Bad to #get(int), #contains(T) : O(n)
  3. How to choose? ArrayList: Stack When we needs random access

    LinkedList: Stack, Queue (java.util.Deque) When we treat head or tail frequently
  4. HashMap<K, V> It use hash table to manage key We

    have to implement #hashCode() correctly It does not implement SortedMap interface
  5. TreeMap<K, V> It use red-black tree to manage key We

    have to implement Comparable or Comparator correctly It implements SortedMap
  6. LinkedHashMap<K, V> A subclass of HashMap It has bidirectional list

    to memory insertion- order (or access-order) It does not implement SortedMap interface
  7. How to choose? HashMap Standard use TreeMap When we have

    to use ordering LinkedHashMap When we have to memory insertion-order