Java-preliminary Understanding-Common Object API (collection framework-generics-generics-limits-the embodiment of the upper limit)

Source: Internet
Author: User
Tags addall

One.

Now to say the app, look at the annotations in the API,

Here's a method AddAll there is Extedns E, let's see when this method is used. Here are three collections, with storage person,student, and worker's.

And then we'll show you this advanced application, if we want to define the set, (describe the collection)

In the case where the element is added in this class, it is written in an E, what type is specified, and what type is added in the Add method. Adding one element at a time is slow, and the AddAll method can add a bunch of elements,

In general, that's how it's written. (AddAll and mycollection added strange, not understand) inside what type (inside refers to the type of the generic after the beginning class), I add a new collection, this collection is what type (API for this method is described, Adds a collection into a collection). Why is it?

Now there is a collection of Al4, which is loaded with a string type, no longer adding an element at a time, using the AddAll method to directly add the string collection. This is not possible, the mycollection<e> in the same type, the AL4 and al1 here are not the same type.

Can I add it without a generic qualification? As shown, remove all generics. It's possible, why?

Because the generic is not specified, it is an object, which can be placed on the inside anyway. In the following example, the number of elements in the output is 3, there are two person objects, and there is a string object. There will be problems when you get it.

Now explain why class start and inside the method defined is the same as the reason for E, if the type does not match, I am clear is person, but to put inside is a string, take the time to turn into a string or person is not clear, there will be an accident, there will be security risks.

The above example is a set that specifies that the type is person, and if there are other elements in the collection that you want to deposit into the collection, you must ensure that the type is consistent, or the compilation will fail, or the development platform will prompt you for an error.

After a while, the person produces some subclass objects, such as the worker is Al2, and now wants to put Al2 in. This is possible, why? Output ptint.size The result is four, when the time is taken according to the person type, because Al2 is placed in the AL1, the time is naturally obtained by AL1. The person can receive student, Mycolleciton This method can accept the new collection to store the E-type element, or the subclass of type E, because there is no problem.

take when there is no type of security risks, AddAll method Extensibility is strong, supposedly said to write an e can be, and now expand the sub-class is also possible.

When do you use this? When stored, the upper limit is used. The storage here may be the position of the mycollection at the beginning of the class.

The element is the object, the upper limit is the type,

Java-preliminary Understanding-Common Object API (collection framework-generics-generics-limits-the embodiment of the upper limit)

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.