Usean array when dealing with data:
Use arrays when working with data:
Fixedin size, or unlikely to grow much
Fixed size, or could not grow too much
Suitablylarge (more than, max, elements, depending on the algorithm)
Appropriate large (more than ten,Ten, three elements, according to the algorithm)
Youwill be doing lots of indexing in it, i.e. you know you'll oftenwant the third element, or the fifth, or whatever.
You will do a lot of indexing, i.e. you know you will often want a third element, or five, or whatever.
Usea list for:
Working with lists
-
variablelength data lists.
-
-
thatare mostly used as a Stack or a queue or need to is iterated in itsentirety
-
This is primarily used as a stack or queue or all
-
whenyou do not want to write a expression to derive the ultimate arraysize for the de Claration and want to wastefully pick alarge number
-
When you don't want to write a table The declaration of the limit array size is deduced, you don't want to waste a lot of
Usea HashMap for:
Use a HashMap:
VariableLength Data lists
Variable length data sheet
Thatneed to is indexed like an array would
This needs to be like an array index
arrayis Fixed in Size. Where, Collection is Grow able in nature.
-
arraystores homogeneous data. Where, Collection store both Homogeneousas well as heterogeneous data. Arrays store uniform data. Where the collection store has both uniform and heterogeneous data.
-
inarray, there is no underlined Data structures, whereas, Collectionhas underlined DS. In the array, there is no underlined data structure, and the collection is underlined.
-
arrayis recommended in performance, whereas Collection isnot. The array is recommended for performance, while the collection is not.
-
arrayuse more memory space compare to Collection. Arrays use more memory space to compare collections.
If you think so, it's easy: collections are better than arrays of objects, which is basically conceivable. You should prefer List<Foo>
Foo[]
over . Consider:
acollection can be mutable or immutable. A nonempty array must alwaysbe mutable. A collection can be mutable or immutable. An empty array must be mutable.
acollection can be thread-safe; even concurrent. An array was neversafe to publish to Multiplethreads. Collections can be thread-safe, even concurrent. An array is unsafe to publish to multiple threads.
acollection can allow or disallow null elements. An array must alwayspermit null elements. A collection can allow or disallow null elements. An array must always allow empty elements.
Acollection is type-safe; a array is not. Because arrays "fake" covariance, arraystoreexception can result atruntime. Collections are type-safe; arrays are not. Because of the "false" covariance of the array, an array store exception can be caused at run time.
-
acollection Can hold a non-reifiable type (e.g. List<class<?extends e>> or list<optional<t>>). With an arrayyou get compilation warnings and confusing runtimeexceptions. A collection can hold a non-generic materialized type (such as a class list << ? or list < Option > Option > ). Using arrays, you can compile warnings and chaotic run-time exceptions.
Acollection have a fully fleshed-out API; an array has onlyset-at-index, Get-at-index andlength. A collection has a flesh and blood API; The array only sets the indicator in the indicator and length.
Acollection can has views (unmodifiable, sublist, filter ...). Nosuch luck for an array. A collection can have comments (non-modifiable lists, filters, ...). )。 There is no such an array of luck.
alist or set ' Equals, Hashcode and toString methods do-what-usersexpect; those methods on a array do anything but WH At your expect--a common source ofbugs. A list or collection of equal,hashcode , and toString methods to do what the user expects; These methods do anything to the array, but what you expect--the common source of errors.
Becauseof All the reasons above, third-party libraries like guava won ' tbother adding much additional support for array s, focusing only oncollections, so there is a networkeffect. For these reasons, third-party libraries like pomegranates do not increase the number of additional support arrays, only focus on the collection, so there is a network effect.
This article is from the "Mr_computer" blog, make sure to keep this source http://caochun.blog.51cto.com/4497308/1699876
The Java collection is better than an array (why do we need a collection for arrays?). )