The Java collection is better than an array (why do we need a collection for arrays?). )

Source: Internet
Author: User

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.

    1. 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.

    2. 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.

    3. arrayis recommended in performance, whereas Collection isnot. The array is recommended for performance, while the collection is not.

    4. 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:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

  8. 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.

  9. 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?). )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.