Vs Array Collection Efficiency

Could you tell me Efficiency , why Array is better Collection ?

+4
source share
5 answers

This is not true. This will depend on the use of your container.

Some algorithms can be executed in O (n) in an array and in O (1) for another type of collection (which implements the Collection interface).

  • Think about deleting an item, for example. In this case, the array, even if the native type, will run slower than the linked list, and calls to its method (which can be nested in any case on some virtual machines): it works in O (n) VS O (1) for the linked list
  • Think about finding an item. It works at 0 (n) for the VS O (log n) array for the tree.

Some Collection implementations use an array to store their elements (ArrayList, I think), so in this case the performance will not differ significantly.

You should spend time optimizing your algorithm (and use the various types of collections available) instead of worrying about the pros / cons of the VS Collection array.

+6
source

Many collections are wrappers for arrays. This includes ArrayList, HashMap / Set, StringBuilder. For optimized code, the difference in operation performance is minimal, except when you come to operations that are better suited for this data structure, for example. map search is much faster than array search.

Using generics for collections, which are mostly primitives, can be slower, and not because the collection is slower, but creating additional objects and using the cache (because the required memory may be higher). This difference is usually too small and significant, but if you can use the Trove4J libraries, which are wrappers for arrays of primitives instead of arrays of objects.

Where collections are slower when you use operations that they are not suitable, for example. random access to LinkedList, but reasonable coding can avoid these situations.

+2
source

Basically, since arrays are primitive data structures in Java. Access to them can be translated directly into their own memory access instructions, rather than method calls.

However, it is not entirely obvious that arrays will strictly outperform collections under any circumstances. If your code refers to collection variables, where the type of runtime can be monomorphically known in JIT time, Hotspot will be able to embed access methods, and where they are simple, it can be just as fast, because in any case there is no overhead.

However, many access methods to collections are more complex than array references. For example, there is no way for HashMap to be as efficient as simply looking for an array, no matter how Hotspot optimizes it.

0
source

You cannot compare these two. ArrayList is an implementation, Collection is an interface. There may be different implementations for the Collection interface.

In practice, an implementation is chosen that is as easy as accessing your data. Usually ArrayList if you need to iterate over all elements. Hashtable if you need key access.

Performance should be considered only after measurements. Then it’s easy to change the implementation, because the collection is based on such common interfaces as the Collection interface.

0
source

The question is which one to use and when?

Array - This is basically a set of elements of a fixed size. The bad point about the array is that it does not change. But its constant size provides efficiency if you are free from the size of your element. Therefore, arrays are best used when you know the number of elements available.

Collection

ArrayList is another set in which the number of elements can be changed. Therefore, if you are not sure about the number of elements in a collection, use an ArrayList. But when using ArrayLists, some facts need to be considered.

  • ArrayLists are not syncing. So if there are several access streams and list changes, then synchronization may be required for processing externally.

  • ArrayList is internally implemented as an array. Therefore, whenever an array of n + 1 elements is added to a new element, then all n elements are copied from the old array to the new array, and then the new element is inserted into the new array.

  • Adding n items is required on time.

  • IsEmpty, size, iterator, set, get, and listIterator operations
    require the same amount of time, regardless of the item you are accessing.

  • Only objects can be added to ArrayList

  • Allows null elements

If you need to add a large number of elements to an ArrayList, you can use the health operation (int minCapacity) to ensure that the ArrayList has the required capacity. This ensures that the array will be copied only once, when all elements are added and will increase the performance of adding elements to the ArrayList. In addition, inserting an element in the middle of 1000 elements will require you to move 500 elements up or down, and then add the element in the middle.

The advantage of using ArrayList is that access to random elements is cheap and does not depend on the number of elements in ArrayList. But adding elements to the head of the tail or in the middle is expensive.

A vector is similar to an ArrayList with the difference that it is synchronized. It offers some other benefits, such as initial capacity and additional features. Therefore, if your vector has a capacity of 10 and incremental capacity of 10, then adding the 11th element will create a new vector with 20 elements, and 11 elements will be copied to the new vector. Thus, adding 12-20 elements will not require the creation of a new vector.

By default, when a vector needs to increase the size of its internal data structure to store more elements, the size of the internal data structure doubles, while for ArrayList, the size increases by only 50%. Therefore, an ArrayList is more conservative in terms of space.

LinkedList is much more flexible and allows you to insert, add and remove elements on both sides of your collection - it can be used as a queue or even a double queue! Internally, LinkedList does not use arrays. LinkedList is a sequence of nodes that are double. Each node contains a header where the objects are actually stored, and two links or pointers to the next or previous node. LinkedList looks like a chain made up of people who hold each other's hands. You can insert people or node into this chain or delete. Linked lists allow node to insert / delete an operation at any point in the list at a constant time.

Thus, the insertion of elements into a linked list (whether on the head or in the tail or in the middle) is small. Also, when you extract items from your head, it is cheap. But if you want to accidentally access the elements of a linked list or access the elements at the tail of the list, then the operations will be difficult. The reason is, to access the n + 1th element, you need to analyze the first n elements in order to reach the n + 1th element.

Also the linked list is not synchronized. Therefore, several threads modifying and reading the list must be synchronized from the outside.

Therefore, the choice of which class to use for creating lists depends on the requirements. ArrayList or Vector (if you need synchronization) can be used when you need to add elements at the end of the list and access elements at random - more access operations than add operations. While LinkedList should be used when you need to do a lot of add / remove operations (items) from the head or in the middle of the list, and your access operations are relatively smaller.

0
source

All Articles