Java Collections Best Practices
Lists:
- Use ArrayList with proper initialization if you don't want thread safe for the collection whenever you add/remove/access objects at end and middle of collection.
- Use Vector with proper initialization if you want thread safe for the collection whenever you add/remove/access objects at end and middle of collection.
- Use LinkedList if you don't want thread safe for the collection whenever you add/remove/access objects at beginning of collection.
- Use synchronized LinkedList if you want thread safe for the collection whenever you add/remove/access objects at beginning of collection.
- Use ListIterator than Iterator and Enumeration for List types
Sets:
- Use HashSet for maintaining unique objects if you don't want thread safe for the collection for all basic(add/remove/access) operations otherwise use synchronized HashSet for thread safe.
- Use TreeSet for ordered and sorted set of unique objects for non-thread safe collection otherwise use synchronized TreeSet for thread safe
Maps:
- Use HashMap for non-thread safe map collection otherwise use Hashtable for thread safe collection.
- Use TreeMap for non-thread safe ordered map collection otherwise use synchronized TreeMap for thread safe.
Tags: java collections, lists, sets, maps, linked list, iterator, treeset, hashset, hashmap
Can't find what you're looking for? Try Google Search!
Comments on "Java Collections Best Practices"