Ii. JavaSE _ set (generic) and javase set generic

Source: Internet
Author: User

Ii. JavaSE _ set (generic) and javase set generic

Generic

I. Overview of generics

1. Preliminary understanding

First, let's look at what is generic?

1 List list = new ArrayList (); // no generic constraints 2 List <String> list = new ArrayList <String> (); // a generic Constraint
View Code

The first line of code does not carry out generic constraints, that is, the list can store objects of the type. Then there will be security risks. Next, we will talk about security risks.

The second line of code isGenericIn other words, the second list can only store data of the String type. Note: After jdk1.7, you can also define it as follows: List <String> list = new ArrayList <> (); that is, the subsequent types can be left empty.

Let's take a look at the Collection API

The e-generation table is composed of elements)

The E represents the generic type.

2. Benefits of generics

  • Improved security (converting runtime errors to compilation)
  • Eliminating the hassle of strong Conversion

 

If any error occurs, please correct it.

Email: it_chang@126.com

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.