The best general purpose or 'primary' implementations are likely ArrayList, LinkedHashMap, and LinkedHashSet. They are marked below as " * ". Their overall performance is better, and you should use them unless you need a special feature provided by another implementation. That special feature is usually ordering or sorting.
Here, "ordering" refers to the order of items returned by an Iterator, and "sorting" refers to sorting items according to Comparable or Comparator.
Interface | HasDuplicates? | Implementations | Historical | ||||
Set | no | HashSet | ... | LinkedHashSet* | ... | TreeSet | |
List | yes | ... | ArrayList* | ... | LinkedList | Vector, Stack | |
Map | no duplicate keys | HashMap | ... | LinkedHashMap* | ... | TreeMap | Hashtable, Properties |
- HashMap has slightly better performance than LinkedHashMap, but its iteration order is undefined
- HashSet has slightly better performance than LinkedHashSet, but its iteration order is undefined
- TreeSet is ordered and sorted, but slow
- TreeMap is ordered and sorted, but slow
- LinkedList has fast adding to the start of the list, and fast deletion from the interior via iteration
- HashSet - undefined
- HashMap - undefined
- LinkedHashSet - insertion order
- LinkedHashMap - insertion order of keys (by default), or 'access order'
- ArrayList - insertion order
- LinkedList - insertion order
- TreeSet - ascending order, according to Comparable / Comparator
- TreeMap - ascending order of keys, according to Comparable / Comparator
While being used in a Map or Set, these items must not change state (hence, it is recommended that these items be immutable objects):
- keys of a Map
- items in a Set
- the stored items implement Comparable
- a Comparator for the stored objects be defined
Resource
http://www.javapractices.com/topic/TopicAction.do?Id=65
No comments:
Post a Comment