Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the right Java collection to use in this case?

I need to hold a large number of elements (500k or so) in a list or a set I need to do high performance traversal, addition and removal. This will be done in a multithreaded environment and I don't care if I gets to see the updates done after traversal began (weakly consistent), what Java collection is right for this scenario?

like image 692
Yazan Jaber Avatar asked Jan 31 '11 10:01

Yazan Jaber


Video Answer


3 Answers

I need to hold a large number of elements (500k or so) in a list or a set I need to do high performance traversal, addition and removal. ... This will be done in a multithreaded environment


ConcrrentSkipListMap - it's not a List but List semantics are practically useless in concurrent environment. It will have the elements sorted in a tree alike structure and not accessible via hashing, so you need some natural ordering (or external via comparator)

If you need only add/remove at the ends of a Queue - ConcurrentLinkedQueue.

Synchronized collections are not suited for multi-threaded environment if you expect even moderate contention. They require full lock holding during the entire traverse operation as well. I'd advise against ConcurrentHashMap, either.

In the end: if you are going for real multi-CPU like 64+ and expect high contention and don't want natural ordering follow the link: http://sourceforge.net/projects/high-scale-lib

like image 156
bestsss Avatar answered Oct 19 '22 06:10

bestsss


Here is a very good article on selecting a collection depending on your application

http://www.developer.com/java/article.php/3829891/Selecting-the-Best-Java-Collection-Class-for-Your-Application.htm

you can try this as well

http://www.javamex.com/tutorials/collections/how_to_choose.shtml

like image 44
GuruKulki Avatar answered Oct 19 '22 07:10

GuruKulki


If traversal == read, and add/remove == update, I'd say that it's not often that a single collection is optimized for both operations.

But your best bet is likely to be a HashMap.

like image 41
duffymo Avatar answered Oct 19 '22 06:10

duffymo