collections in JDK- basic data structures you should know -2012/Aug/29 Kengo TODA
View Slide
Basic collectionListSetMap
ArrayListIt just wraps an arrayGood to #get(int): O(1)Bad to #remove(int), #contains(T) : O(n)
LinkedListSimple bidirectional listIt costs more Java heap than ArrayListGood to #remove & #add for head/tail: O(1)Bad to #get(int), #contains(T) : O(n)
How to choose?ArrayList:StackWhen we needs random accessLinkedList:Stack, Queue (java.util.Deque)When we treat head or tail frequently
HashMapIt use hash table to manage keyWe have to implement #hashCode() correctlyIt does not implement SortedMap interface
TreeMapIt use red-black tree to manage keyWe have to implement Comparable orComparator correctlyIt implements SortedMap
LinkedHashMapA subclass of HashMapIt has bidirectional list to memory insertion-order (or access-order)It does not implement SortedMap interface
How to choose?HashMapStandard useTreeMapWhen we have to use orderingLinkedHashMapWhen we have to memory insertion-order
Set ≒ MapHashSet → HashMapTreeSet → TreeMapLinkedHashSet → LinkedHashMap
Legacy implementationsVectorStackHashtableProperties@Deprecated
Additional collectionGoogle guava contains useful collectionsTableMultisetMultimap
ReferencesSource code (OpenJDK, Google guava)