Which collection is best to use? Obviously, there is no single answer that can fit all States. Anyway, here are some general recommendations. By following these recommendations, we can narrow down the range of collection choices.
when using collection class, use the interface .
As with all Java programming, interfaces can be separated from the implementation details. By using interfaces, program designers can easily re-compose the program to use different collection implementations simply by modifying the initial program code.
using collection that have not been synchronized will have a small performance boost .
This may surprise many developers---to understand the performance issues that lock obtains, see chapter 14th. To put it simply, the performance problem of getting lock will only happen at the time of competition. However, collection that have not been synchronized should not compete for lock. If there is competition, then the race condition is more likely to be a problem than performance.
-
for many competing algorithms, consider using concurrent collection instead.
j2se5.0 's collection to replace the synchronized JDK1.2 class collection. Concurrent collection has a better optimization of multi-threaded access.
for the producer/consumer model of the program, consider using queue as collection .
There are many reasons why a queue is best suited to the producer/consumer model. First, the queue provides a sort of request that can prevent data starvation. Second, the queue is highly optimized, with minimal synchronization, atomic access, and in many cases even secure parallel access. With these collection, a large number of threads can also operate in parallel and only have a small bottleneck in access to the queue.
if possible, minimize the use of explicit synchronization . Iterator and other supported method that need to traverse the entire collection may provide more synchronization than collection. This can be a problem when there are many threads involved.
restrict the use of iterator to collection such as Copy-on-write.
First, these classes are used only when the number of elements in the collection is small, which is due to the time and space required for the copy-on-write operation. Second, your program does not need collection to have the most up-to-date information, iterator only with collection information at the time of creation.
consider using multiple collection.
Although some collection have minimal synchronization, these synchronization processes are problematic when there are multiple threads involved. Consider using
Multiple segmented Collecton algorithms are used to replace multiple threads using the same collection general implementation.
There is a slight difference between set and map .
In theory, there are several differences between set and map, but only a little bit different from the point of view of implementation. Many of the set collection are implemented using Map collection. This means that the selection is not actually an option: the element stored in set is almost the key stored in the map.
How to choose Collection Class--java thread in Java (3rd edition)