$30 off During Our Annual Pro Sale. View Details »

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. collections in JDK
    - basic data structures you should know -
    2012/Aug/29 Kengo TODA

    View Slide

  2. Basic collection
    List
    Set
    Map

    View Slide

  3. ArrayList
    It just wraps an array
    Good to #get(int): O(1)
    Bad to #remove(int), #contains(T) : O(n)

    View Slide

  4. LinkedList
    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)

    View Slide

  5. How to choose?
    ArrayList:
    Stack
    When we needs random access
    LinkedList:
    Stack, Queue (java.util.Deque)
    When we treat head or tail frequently

    View Slide

  6. HashMap
    It use hash table to manage key
    We have to implement #hashCode() correctly
    It does not implement SortedMap interface

    View Slide

  7. TreeMap
    It use red-black tree to manage key
    We have to implement Comparable or
    Comparator correctly
    It implements SortedMap

    View Slide

  8. LinkedHashMap
    A subclass of HashMap
    It has bidirectional list to memory insertion-
    order (or access-order)
    It does not implement SortedMap interface

    View Slide

  9. How to choose?
    HashMap
    Standard use
    TreeMap
    When we have to use ordering
    LinkedHashMap
    When we have to memory insertion-order

    View Slide

  10. Set ≒ Map
    HashSet → HashMap
    TreeSet → TreeMap
    LinkedHashSet → LinkedHashMap

    View Slide

  11. Legacy implementations
    Vector
    Stack
    Hashtable
    Properties
    @Deprecated

    View Slide

  12. Additional collection
    Google guava contains useful collections
    Table
    Multiset
    Multimap

    View Slide

  13. References
    Source code (OpenJDK, Google guava)

    View Slide