JAVA course 42nd-generic (II)-generic interfaces & wildcard applications, java
I. Generic Interfaces
Interface Inter <T> {public void show (T t);} class InterImple implements Inter <String >{// it is a String type public void show (String str) {System. out. println ("show" + str) ;}} class InterImple_2 <Q> implements Inter <Q >{// what type is unknown, public void show (Q q) {System. out. println ("InterImple_2.show () +" + q) ;}} public class Main {public static void main (String [] args) {InterImple in = new InterImple (); in. show ("sf"); InterImple_2 <Integer> in2 = new InterImple_2 <Integer> (); in2.show (2 );}}
Ii. Generic limitations
Wildcard. Generally speaking, no matter what type, a symbol is used to represent T, E, X ,?...
Where are the wildcards? Not much different from the wildcard T-Area
Import java. util. arrayList; import java. util. collection; import java. util. hashSet; import java. util. iterator; public class Main {public static void main (String [] args) {ArrayList <String> al = new ArrayList <String> (); al. add ("ads"); al. add ("sfdf"); show (al); ArrayList <Integer> a3 = new ArrayList <Integer> (); a3.add (5); a3.add (6 ); show (a3); HashSet <Integer> Al = new HashSet <Integer> (); al2.add (1); al2.add (2); show ( 2);} // if it is simply printed, what are the wildcards used? Public static void show (Collection <?> Al) {Iterator <?> It = al. iterator (); Iterator <?> It = al. iterator (); while (it. hasNext () {System. out. println (it. next () ;}/// if you need to return the continue operation // public static <T> T show (Collection <T> al) {// Iterator <T> iterator = al. iterator (); // T t = iterator. next (); // return t ;//}}
About iterator
/* Iterator <T> it = al. iterator (); while (it. hasNext () {System. out. println (it. next ();} * // a more elegant for each loop is used after java5.0. iterator and for (T x: al) {System. out. println (x );}
Concise and clear
Demonstration of the first wildcard character:
Import java. util. arrayList; import java. util. collection; import java. util. iterator; public class Main {public static void main (String [] args) {ArrayList <Worker> al = new ArrayList <Worker> (); al. add (new Worker ("abc", 12); al. add (new Worker ("dgh", 11); show (al); ArrayList <Student> a3 = new ArrayList <Student> (); a3.add (new Student ("ASD", 156); a3.add (new Student ("AFDFD", 16); show (a3 );} // if you only want to print Man's subclass // Collectio N <Man> NO, Collection <Man> = new ArrayList <Student> (); the left and right types do not match // so in the generic declaration, you can use the inherited public static void show (Collection <? Extends Man> al) {// Iterator <? Extends Man> it = al. iterator (); while (it. hasNext () {Man man = it. next (); System. out. println (man) ;}}// equivalent to public static <T extends Man> void show1 (Collection <T> al) {// for (T xT: al) System. out. println (xT );}}
Therefore, there are two limits for generic storage:
Upper Limit :? Extends E ,? Only subclasses of the E type or E type can be accepted.
Lower limit :? Super E: accept the parent class of E type or E.
Iii. Upper Limit:
Import java. util. arrayList; import java. util. collection; import java. util. iterator; public class Main {public static void main (String [] args) {ArrayList <Man> al1 = new ArrayList <Man> (); al1.add (new Man ("ABC ", 12); al1.add (new Man ("DGH", 11); ArrayList <Worker> Al = new ArrayList <Worker> (); al2.add (new Worker ("abc ", 12); al2.add (new Worker ("dgh", 11); ArrayList <Student> a0 = new ArrayList <Student> (); al3.add (new Student ("abc ", 12); al3.add (new Student ("dgh", 11); al1.addAll (Al); // The upper limit is applied when elements are stored, because the data is extracted according to the upper limit type, there is no type security risk System. out. println (al1.size ());}}
For more information, see the API documentation. addAll method: addAll (? Extends E), which can store all subclasses of E and E.
Iv. lower limit:
The lower limit can be used to retrieve elements from a set.
That is, no matter what type is stored (as long as it is a subclass of the current parent class), you can use the parent type to receive
Import java. util. comparator; import java. util. treeSet; import java. util. iterator; class ComparaName implements Comparator <Man >{// sort by name public int compare (Man o1, Man o2) {// TODO Auto-generated method stubint t = o1.getName (). compareTo (o2.getName (); return t = 0? O1.getAge ()-o2.getAge (): t ;}} public class Main {public static void main (String [] args) {TreeSet <Man> al1 = new TreeSet <Man> (new ComparaName (); al1.add (new Man ("ABC", 12 )); al1.add (new Man ("DGH", 11); TreeSet <Worker> Al = new TreeSet <Worker> (); al2.add (new Worker ("abc", 12 )); al2.add (new Worker ("dgh", 11); TreeSet <Student> a0 = new TreeSet <Student> (new ComparaName (); al3.add (new Student ("abc ", 12); al3.add (new Student ("dgh", 11); // Add students and workers to the al1 set al1.addAll (_3); al1.addAll (Al ); // Iterator <Man> it = al1.iterator (); while (it. hasNext () System. out. println (it. next ());}}
5. wildcard characters
Import java. util. collection; import java. util. arrayList; import java. util. iterator; public class Main {public static void main (String [] args) {ArrayList <Man> al1 = new ArrayList <Man> (); al1.add (new Man ("ABC ", 12); al1.add (new Man ("DGH", 11); ArrayList <Man> Al = new ArrayList <Man> (); al2.add (new Man ("ABC ", 12); al2.add (new Man ("DGH", 11); boolean flag1 = al1.containsAll (Al); System. out. println (flag1); ArrayL Ist <String> a0 = new ArrayList <String> (); al3.add ("qwertyi"); al3.add ("asd"); boolean flag2 = al1.containsAll (_3); System. out. println (flag2);} // method: containsAll (Collection <?> C )//? The equals method in the Object is "asd ". equals (new Worker ("asd", 12), both compile and run through the public static void show (Collection <?> Al) {for (Iterator <?> It = al. iterator (); it. hasNext ();) {System. out. println (it. next ());}}}
Java interface generics
The second code, private SeqList <Element> list, defines a set of class elements.
The second Code also calls the set of class elements in the constructor of class SeqSparseMatrix.
I am very grateful to you for learning the generic problems java has encountered.
In general, generic is to pass the type of a class to a class, interface, or method as a parameter.
The generic function is to avoid tedious packing and unpacking.
For example, List <A> aList = new ArrayList <A> ();
Add aList. add ()
Directly obtain
For (Object o: aList ){
O; // is the object you saved
}
If you do not need generics
List <> aList = new ArrayList <> ();
You must perform a conversion when retrieving data.
For (Object o: aList ){
A o1 = (A) o; // o1 is the object you want
}