1. simple use of generics and meaning 2. generic upper and lower limits 3. generics and subclass inheritance limit 4. generic classes and generic methods 5. generic nesting and generic erase generics (Generic) What is generics: ?java5 begins an extension of the Java language type to support the creation of classes that can be parameterized by type. You can think of a type parameter as a type placeholder that you specify when you use a parameter type, as if the form parameter of the method is the placeholder for the actual parameter. ? generics Ensure the type safety and good maintainability of large applications; The advantages of using generics:? Type safety, which allows the compiler to limit the types defined by the generic type. such as ensuring that the elements in the treeset are of the same type must be consistent; eliminate forced-type conversions, such as, to make When comparing with comparable , you need a type strong; generic class a class declaration by an identifier to represent the type of a field in a class or the type of a method's return value or parameter. This is OK when the class is declared or instantiated as long as you specify the type you want. declaring a class with generics: class class name < generic type 1, generic type 2......>{ generic type variable name, generic type method name () {} return value type method name (generic type variable name) { } } classes with generics: class name < concrete class > Object name = new Class Name < concrete class > (); Note: TYPE parameter specification: Recommended use specification-common generics, generics only exist in the source file,class file does not exist, that is, in the compilation phase is lost, the basic data type cannot be a generic type; k key, such as the type of key key of the map v values, such as the value of map value type e element, such as Set<e> element represents elements, types of elements t generics, type meaning thinking list<object> l and list<string> l can run wildcard the generic type must match to be passed when the reference is passed, otherwise the compilation does not pass; A generic object that represents an unknown type:? list<?> represents the list collection of unknown elements; This list of wildcard characters only represents the parent class of various generic lists, and cannot add elements to the collection; ? list<?> list = new arraylist<> (); List.add (1);//error compiler cannot infer based on information type public void show (list<?> list) {} ?//indicates that a List collection of any type can be accepted Upper and lower bounds for generics set the upper bound of a generic object use extends, which means that the parameter type can only be a subclass of that type or type: ? Declaration object: Class name < extends class > object name ? Definition class: Class name < generic label extends class >{} set the lower bound of a generic object use Super, Indicates that the parameter type can only be the type or parent of the type: ? Declaration object: Class name <? super class > object name ? Definition class: Class name < generic label extends class >{} public static void show (List <? Extends number> l) { } public static void show (LIST<? Super string> L) { } public static void Main (string[] args) {person<integer> P1 = new Person<> (); P1.setval (; person<double>) P2 = new person<> (); P2.setval (3.14); person<string> p3 = new person<> (); P3.setval ("007"); show (p1);//√ show (p2);//√ show (p3);//x } public static void Show ( person<? Extends number> p) {System.out.println (P.getval ()); } public static void Main ( String[] args) {person<integer> P1 = new person<> (); P1.setval (in);//integer person<double> P2 = New Person<> (); P2.setval (3.14);//double person<string> p3 = new person<> (); P3.setval ("007");//string person<object> P4 = new person<> (); P4.setval (New Object ());//object show (p1);//x show (p2);//x show (p3);//√ show (p4);//√ } public static void Show (PERSON<? super string> p) {System.out.println (P.getval ()); } After a generic interface java5, you can declare a generic interface the same way you declare a generic class. ? public interface idao<t>{} generic interface subclasses are available in two ways: ? Declare generics directly after subclasses, ? The specific generic type is given in the interface implemented by the subclass & Nbsp;public class Daoimpl<t> implements Idao<t>{ } public class Daoimpl implements idao< String> { } Generic methods methods can define generic parameters, and parameter types of parameters are the types of arguments. Format:?< generic label > return value type method name ([generic label parameter] ...) public static <T> t show (t param) {return param;} .....main ..... {System.out.println (Show (New Date ())); System.out.println (Show ("Cditcast")); } generics use return generic object &NBSP by generic method; The method return type explicitly gives the concrete type uniform incoming parameter type generic array public <T> t[] Show (t[] ts) { } public <T> void Show (T ... ts) { } generics nested can point to a generic: of another class from a generic type of one class public class Demo1 { public static void main (string[] args) {map<string,string> Map = new hashmap<> (); m Ap.put ("1", "A"); map.put ("2", "B"); Map.put ("3", "C"); Map.put ("4", "D"); set<map.entry<string, string>> Set = Map.entryset (); iterator<map.entry<string, String >> it = Set.iterator (); while (It.hasnext ()) { map.entry<string, string> Entry = It.next (); System.out.println (Entry.getkey () +--+ entry.getvalue ()); } } } generic erase in strict generic code, classes with generic declarations should always take type parameters. However, in order to be consistent with the old Java code, it is also allowed to not specify a type parameter when using a class with a generic declaration, if the type parameter is not specified for the generic class, the type parameter is called a primitive type, the default is the maximum type specified when the declaration parameter is; when the When an object with generic information is assigned to another variable that does not have generic information, all type information between the angle brackets is discarded. ? For example, if the list<string> type is converted to a list, the list changes the type check of the collection element to the upper bound of the variable, object. Erase and convert of generics class num<t extends Number>{private t t;public Num (T t) {this.t= T;} .... Getter/setter .... }public class Demo{public static void Main (String[] args) {num<integer> n= new num<> (5); Integer i= N.get T (); Num N2 = n;//will lose the generic information number num= N2.gett ();//integer i= N2.gett ();}} public class Demo{public static void Main (string[] args) {List<integer> li= new arraylist<> (); Li.add (1); list<string> ls= null;//ls= li; Cannot convert list list= li;ls= list;//no error, only unchecked warning, when L ist actually refers to list<integer> System.out.println ("--" + ls.get (0));//attempt to remove as a string object}}
Generics (Java Beginner's class notes)