List - this is an ordered list of objects, insertion order is maintained and retrieval order is in the list order but items can also be random accessed, duplicate items are allowed, generally allow storage of null values (the ones below do), generally fast to iterate and find items by position but slow to do lookups
- ArrayList - Unsychronized, nulls allowed (fastest)
- Vector - Synchronized, only slightly slower in tests of sizes under 100000
- Stack - Synchronized, same speed as Vector, LIFO queue
- LinkedList - Unsynchronized, allows two way iteration and modification of items (like a stack or queue)
- CopyOnWriteArrayList - Synchronized, significantly slower in tests of large numbers of items or average list changes, only slightly slower when used with very small numbers (<100)>
- HashSet - Unsychronized (fastest), slower than HashMap which it is built on, allows nulls
- LinkedHashSet - Unsychronized, ordered by insertion, allows nulls
- TreeSet - Unsychronized, ordered by the natural ordering of the items or a comparator provided at construction, allows nulls but there are issues with removing them
- CopyOnWriteArraySet - Synchronized, significantly slower in tests of large numbers of items or average set changes, only slightly slower when used with very small numbers (<100)>
- IdentityHashMap - Unsychronized (fastest), uses reference equality (==) instead of object equality (equals) to compare keys, actually violates the Map interface guarantee, all iterators are unordered, allows null keys and values
- HashMap - Unsychronized, this is the fastest general purpose map, all iterators are unordered, allows null keys and values
- ConcurrentHashMap - Synchronized, all iterators are unordered, does not allow null keys or values
- Hashtable - Synchronized, all iterators are unordered, does not allow null keys or values
- LinkedHashMap - Unsychronized, all iterators are ordered based on insertion order of the original key (does not change if a key is reinserted), allows null values but null keys are not allowed
- TreeMap - Unsychronized, iterators are ordered by the natural or comparator ordering of the keys, allows null keys and values but the comparator needs to understand them
16 comments:
Great and useful post, megacheers!
-steve
jav-collections.blogspot.com
jav-collections.blogspot.com
jav-collections.blogspot.com
javolution contains some very fast implementation of collections ( javolution.org )
I'm sorry - this seems entirely out of context. What does "fast" mean? For inserts? Removals? Bulk operations. Serial operations? Concurrent Operations?
I must agree with Taylor – seems a little bit out of context – for example: ArrayList is fast as long as you know the size in advance (or as long as you don’t have many resizing) and as long as you don’t have many removals. On the other hand there are memory considerations – LinkedList has the pointers overhead but a large ArrayList might require a big chunk of contiguous memory to be allocated on a resize operation, also a large empty ArrayList is a big waste of memory.
Same for CopyOnWriteArrayList – the performance issue in here is also depending on the usage: modification of the list might cost (it is synchronized and allocates a new list) however it is very efficient for concurrent iteration and you are safe from the concurrent modification exception.
Quite superficial summary and presentation.
If one is to choose collection implementations without really understanding data structures, then the next best thing is a thorough decision tree, which at its leaves have all collections. That would be very helpful for such programmers. Yes, it would be tedious to construct this (correctly), but it would really offer something useful that could be used as a reference.
Great post for ready reference. Every now and then developer faces the question - which collection to use??? This might help him to decide
Nicely put together. I use Collections on a daily basis; yet I learned something here.
Dimitris made a good proposition. The "decision tree" would be a great follow-up.
I agree with Taylor, and showing what is the test code or at least methodology could have been better.
FYI, you should check for typos (Unsychronized...).
Yes, it is a useful introduction for newcomers to Java (I searched such information at the time) in compact form.
I wonder why you link to Java 1.5 in 2009... :-) You have probably your reasons, but perhaps you should mention why.
If you actually want timings to verify these assertions, see Kent Beck's book, "Implementation Patterns" in which he does numerous timins and graphts the results.
Sun Java is one of the most flexible platform for application development Sun Java development gives the way to develop complex applicaton development.
Really good quick reference
Cool article about sets performance http://blog.griddynamics.com/2011/03/ultimate-sets-and-maps-for-java-part-i.html
Post a Comment