Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

default Collection type

Assume you need to store/retrieve items in a Collection, don't care about ordering, and duplicates are allowed, what type of Collection do you use?

By default, I've always used ArrayList, but I remember reading/hearing somewhere that a Queue implementation may be a better choice. A List allows items to be added/retrieved/removed at arbitrary positions, which incurs a performance penalty. As a Queue does not provide this facility it should in theory be faster when this facility is not required.

I realise that all discussions about performance are somewhat meaningless, the only thing that really matters is measurement. Nevertheless, I'm interested to know what others use for a Collection, when they don't care about ordering, and duplicates are allowed, and why?

like image 435
Dónal Avatar asked Aug 18 '10 08:08

Dónal


People also ask

What is collection type of collection?

Collection types represent different ways to collect data, such as hash tables, queues, stacks, bags, dictionaries, and lists. All collections are based on the ICollection or ICollection<T> interfaces, either directly or indirectly.

What are collection types in Swift?

Swift provides three primary collection types, known as arrays, sets, and dictionaries, for storing collections of values. Arrays are ordered collections of values. Sets are unordered collections of unique values. Dictionaries are unordered collections of key-value associations.

What is the difference between collections and collections?

Collection is used to represent a single unit with a group of individual objects whereas collections is used to operate on collection with several utility methods. Since java 8, collection is an interface with static as well as abstract and default methods whereas collections operate only with static methods.


3 Answers

"It depends". The question you really need to answer first is "What do I want to use the collection for?"

If you often insert / remove items on one of the ends (beginning, end) a Queue will be better than a ArrayList. However in many cases you create a Collection in order to just read from it. In this case a ArrayList is far more efficient: As it is implemented as an array, you can iterate over it quite efficient (same applies for a LinkedList). However a LinkedList uses references to link single items together. So if you do not need random removals of items (in the middle), a ArrayList is better: An ArrayList will use less memory as the items don't need the storage for the reference to the next/prev item.

To sum it up:

ArrayList = good if you insert once and read often (random access or sequential)

LinkedList = good if you insert/remove often at random positions and read only sequential

ArrayDeque (java6 only) = good if you insert/remove at start/end and read random or sequential

like image 185
redCube Avatar answered Oct 03 '22 00:10

redCube


As a default, I tend to prefer LinkedList to ArrayList. Obviously, I use them not through the List interface, but rather through the Collection interface.

Over the time, I've indeed found out that when I need a generic collection, it's more or less to put some things in, then iterate over it. If I need more evolved behaviour (say random access, sorting or unicity checks), I will then maybe change the used implementation, but before that I will change the used interface to the most appropriated. This way, I can ensure feature is provided before to concentrate on optimization and implementation.

like image 44
Riduidel Avatar answered Oct 03 '22 01:10

Riduidel


ArrayList basicly contains an array inside (that's why it is called ArrayList). And operations like addd/remove at arbitrary positions are done in a straightforward way, so if you don't use them - there is no harm to performance.

like image 27
falagar Avatar answered Oct 03 '22 02:10

falagar