Common features and benefits of generics

Common features of generics

Generics are a feature after Java SE 1.5. The definition of generics in "Java Core Technology" is:
"Generic" means that the written code can be reused by different types of objects.
"Generic", as the name implies, "generic type". We provide the concept of general reference, but specific rules can be used to
constrain the specific implementation . For example, we use a lot of ArrayList is a generic class, ArrayList as a collection can store various elements, such as
Integer, String, Various custom types, etc., but when we use them, we use specific rules to constrain them. For example, we can constrain the
collection to store only Integer type elements, such as

List<Integer> iniData = new ArrayList<>()

Benefits of using generics?

Exemplified in sets, using the generic benefit is because we do not add different types of elements defined collection of different types, such as integers set
engagement type, floating-collections, collections string, we can define a set of Store integer, floating point, and string data, but
this is not the most important, because we only need to set the underlying storage to Object, and all added data can be up-cast to
Object. More importantly, we can control the type of data stored in accordance with our own ideas through rules.

Guess you like

Origin blog.csdn.net/m0_51684972/article/details/109172434