Introduction to Generics
The main reason for increasing the generic mechanism in Java SE 5.0 was to meet one of the earliest Java specification requirements (JSR 14) that was customized in 1999. The expert group spent about five years defining specifications and testing implementations. program code written using the generic program has better security and readability than the clutter of code that uses object variables and then enforces type conversions, which is especially useful for collections.
Why you should use generic programming
Generic Programming (Generic programming) means that the code you write can be reused by many different types of objects.
Prior to Java SE 5.0, Java generic programming was implemented with inheritance. The ArrayList class maintains only an array of object references:
Public class // before Java SE 5.0 { public Object get (int i) {...} Public void Add (Object o) {...} ... Private object[] Add (Object o);}
There are two problems with this implementation. The first problem is that when you get a value, you have to force the type conversion, and the second problem is that there is no error checking here, so you can add objects of any class to the collection ArrayList. With the generics introduced in Java SE 5.0, it provides a better solution: type parameter (parameters). The ArrayList class has a type parameter that indicates the type of element, making the program more readable and secure.
Generic programming is divided into three stages, the basic level is:
1. Use only generic classes-Typically, a collection like ArrayList, regardless of the way they work;
2. When mixing different model classes together, you may see ambiguous error messages, so you need to learn Java generics to solve these problems systematically;
3. Finally, you may want to implement your own generic class and generic method;
Definition of a simple generic class
A generic class (generic Class) is a class that has one or more type variables. For example:
PackageCn.net.bysoft; Public classPair<t> { PublicPair () { This. First =NULL; This. Sceond =NULL; } PublicPair (t first, T Sceond) {Super(); This. First =First ; This. Sceond =Sceond; } PublicT GetFirst () {returnFirst ; } Public voidSetfirst (T first) { This. First =First ; } PublicT Getsceond () {returnSceond; } Public voidSetsceond (T sceond) { This. Sceond =Sceond; } PrivateT first; PrivateT Sceond;}a simple generic class
The pair class introduces a variable t, enclosed in angle brackets, and placed behind the class name. A generic class can have more than one type variable. For example, the first and second fields in the pair class can use different types:
Public class Pair<t, u> {...}
Note: Type variables are capitalized and relatively short, which is common in Java libraries, where variable e is used to represent the element type of the collection, and K and V represent the type of the table's keywords and values, and T (which can also be used with adjacent letters U and s) to represent any type. In other words, a generic class can be seen as a common class factory, using the following methods:
PackageCn.net.bysoft;//find the smallest and largest element in the array. Public classArrayalg { Public StaticPair<string>Minmax (string[] a) {if(A = =NULL|| A.length = = 0) return NULL; String min= A[0]; String Max= A[0]; for(inti = 0; i < a.length; i++) { if(Min.compareto (a[i]) > 0) min =A[i]; if(Max.compareto (A[i]) < 0) Max =A[i]; } return NewPair<string>(min, max); }} PackageCn.net.bysoft; Public classPairtest { Public Static voidMain (string[] args) {//TODO auto-generated Method Stubstring[] Words = {"Mary", "had", "a", "little", "lamb" }; Pair<String> mm =Arrayalg.minmax (words); System.out.println (Mm.getfirst ()); System.out.println (Mm.getsceond ()); }}examples of simple generic class use
Generic methods
As explained earlier how to define a generic class, you can actually define a simple method with a type parameter.
Package Cn.net.bysoft; Public class Arrayalg { publicstatic <T> T getmiddle (t[] a) { return A[A.LENGTH/2];} }
A generic method can be defined in a normal class that, when called, puts a specific type in the angle brackets before the method name:
string[] Words = {"Mary", "had", "a", "little", "lamb"= arrayalg.<string>getmiddle (words);
In most cases, the <String> type parameter can be omitted from the method invocation. The compiler has enough information to infer the method being called. It is matched with the generic t[] using the names type (i.e., string[]) and the T must be a string. In other words, you can call:
String Word = arrayalg.getmiddle (words);
Java reading notes-generics