The best general purpose or ‘primary’ implementations are likely ArrayList, LinkedHashMap, and LinkedHashSet. 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.
|Map||no duplicate keys||HashMap||…||LinkedHashMap*||…||TreeMap||Hashtable,Properties|
Principal features of non-primary implementations :
- HashMap has slightly better performance than LinkedHashMap
- HashSet has slightly better performance than LinkedHashSet
- 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
Iteration order for above implementations :
- 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
For LinkedHashSet and LinkedHashMap, the re-insertion of an item does not affect insertion order.
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
Sorting requires either that :
To retain the order of a ResultSet as specified in an ORDER BY clause, insert the records into a List or a LinkedHashMap.
Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code
Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!
To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.