0% found this document useful (0 votes)
77 views1 page

Java Collection Matrix

Java Collection Matrix

Uploaded by

Mohammad Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
77 views1 page

Java Collection Matrix

Java Collection Matrix

Uploaded by

Mohammad Ali
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Random Key-Value Allows Allows Null Blocking Upper

Ordering Thread Safe


Access Pairs Duplicates Values Operations Bounds Usage Scenarios
Most Commonly Known Collections
* Default choice of List implementation
* To store a bunch of things
* Repetitions matters
ArrayList YES YES NO YES YES NO NO NO
* Insertion order matters
* Best implementation in case of huge lists which are read intensive
(elements are accessed more frequently than inserted deleted)
* Default choice of Map implementation
HashMap NO YES YES NO YES NO NO NO
* Majorly used for simple in-memory caching purpose.
* Historical implementation of List
Vector YES YES NO YES YES YES NO NO
* A good choice for thread-safe implementation
* Similar to HashMap
Hashtable NO YES YES NO NO YES NO NO * Do not allow null values or keys
* Entire map is locked for thread safety
Most Talked About Collections
* To store bunch of things
* A very nice alternative for ArrayList if
HashSet NO YES NO NO YES NO NO NO
** Do not want repetitions
** Ordering does not matter
* To store bunch of things in sorted order
* A very nice alternative for ArrayList if
TreeSet YES YES NO NO NO NO NO NO
** Do not want repetitions
** Sorted order
* Sequential Access
* Faster adding and deleting of elements
* Slightly more memory than ArrayList
LinkedList YES NO NO YES YES NO NO NO
* Add/Remove elements from both ends of the queue
* Best alternative in case of huge lists which are more write intensive
(elements added / deleted are more frequent than reading elements)
* Random Access
* Faster searching and retrieval of elements
ArrayDeque YES YES NO YES NO NO NO NO
* Add/Remove elements from both ends of the queue
* Best alternative in case of huge lists which are more read intensive
* Similar to a Vector
Stack YES NO NO YES YES YES NO NO
* Last-In-First-Out implementation
TreeMap YES YES YES NO NO NO NO NO * A very nice alternative for HashMap if sorted keys are important
Special Purpose Collections
* The keys that are not referenced will automatically become eligible for
garbage collection
WeakHashMap NO YES YES NO YES NO NO NO
* Usually used for advanced caching techniques to store huge data and
want to conserve memory
* A Utility class provided to manipulate arrays
** Searching
Arrays YES YES NO YES YES NO NO YES
** Sorting
** Converting to other Collection types such as a List
* Properties are exactly same as the Hashtable
* Keys and Values are String
Properties NO YES YES NO NO YES NO NO
* Can be loaded from a input stream
* Usually used to store application properties and configurations
Thread Safe Collections
* A thread safe variant of ArrayList
* Best use for
CopyOnWriteArrayList YES YES NO YES YES YES NO NO
** Small lists which are read intensive
** requires thread-safety
* A thread safe variant of Hashtable
* Best use for
ConcurrentHashMap NO YES YES NO NO YES NO NO
** requires thread-safety
** Better performance at high load due to a better locking mechanism
* A thread safe variant of TreeMap
ConcurrentSkipListMap YES YES YES NO NO YES NO NO * Best use for
** requires thread-safety
* A thread safe variant of TreeSet
* Best use for
ConcurrentSkipListSet YES NO NO NO NO YES NO NO ** Do not want repetitions
** Sorted order
** Requires thread-safety
* A thread-safe implementation of a Set
* Best use for
CopyOnWriteArraySet YES YES NO NO YES YES NO NO ** Small lists which are read intensive
** requires thread-safety
** Do not want repetitions
* A thread-safe variant of PriorityQueue
* Best use for
ConcurrentLinkedQueue YES NO NO YES NO YES NO NO ** Small lists
** No random access
** requires thread-safety
"* A thread-safe variant of LinkedList
* Best use for
** Small lists
ConcurrentLinkedDeque YES NO NO YES NO YES NO NO
** No random access
** Insertions, retrieval on both sides of the queue
** requires thread-safety"
Blocking Collections
* Best use for Producer - Consumer type of scenarios with
** Lower capacity bound
ArrayBlockingQueue YES NO NO YES NO YES YES YES
** Predictable capacity
* Has a bounded buffer. Space would be allocated during object creation
* Best use for Producer - Consumer type of scenarios with
** Large capacity bound
LinkedBlockingQueue YES NO NO YES NO YES YES YES
** Unpredictable capacity
* Upper bound is optional
* Can be used in situations where the producers should wait for consumer
LinkedTransferQueue YES NO NO YES NO YES YES YES
to receive elements. e.g. Message Passing
"* Best use for Producer - Consumer type of scenarios with
** Large capacity bound
PriorityBlockingQueue YES NO NO YES NO YES YES NO
** Unpredictable capacity
** Consumer needs elements in sorted order
* A Deque implementation of LinkedBlockingQueue
LinkedBlockingDeque YES NO NO YES NO YES YES YES
** Can add elements at both head and tail
* Both producer and consumer threads will have to wait for a handoff to
occur.
SynchronousQueue YES NO NO YES NO YES YES NO
* If there is no consumer waiting. The element is not added to the
collection.
* Similar to a normal LinkedBlockingQueue
DelayQueue YES NO NO YES NO YES YES NO * Elements are implementations of Delayed interface
* Consumer will be able to get the element only when it's delay has expired
Source: http://www.janeve.me/articles/which-java-collection-to-use

You might also like