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?
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.
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.
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.
"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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With