Java Learning Lesson 42nd-Generics (ii)-generic interface && wildcard application

Source: Internet
Author: User
Tags addall

One, generic interface

Interface inter<t>{public void Show (T t);} Class Interimple implements inter<string>{//knows is the string type public void show (String str) {System.out.println ("show" +str );}} Class Interimple_2<q> implements inter<q>{//do not know what type, when used to know 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 Qualification


Generic wildcard characters, in layman's words, use a symbol to represent t,e,x, regardless of type,?...
Where are the wildcard characters? Not quite the same as wildcard T

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 ("Ad S "); Al.add (" sfdf "); show (AL); arraylist<integer> a3 = new arraylist<integer> () A3.add (5); A3.add (6); show (A3); hashset<integer> Al2 =  new Hashset<integer> () Al2.add (1); Al2.add (2); show (Al2);} If printing is simple, wildcard characters are 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 to continue operation//public Static <T> t Show (collection<t> al) {//iterator<t> Iterator = Al.iterator ();//t t = Iterator.next ();//return t;//}}

about Iterators

/*iterator<t> it = Al.iterator (); while (It.hasnext ()) {System.out.println (It.next ());} *///java5.0 later used a more elegant for Each loop, with iterator to represent the same loop for (T x:al) {System.out.println (x);}

Simple and straightforward
Wildcard character The first demo:

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"); Al.add ( New Worker ("dGH"); show (AL); arraylist<student> a3 = new arraylist<student> () A3.add (New Student ("ASD", 156)); A3.add (New Student (" AFDFD ","); show (A3);} If you only want to print the subclass of man//collection<man> not,collection<man> = new arraylist<student> (); left and right types do not match//So in generic declarations, You can take the inherited way public static void show (COLLECTION<? extends Man> al) {//iterator<? extends man> it = Al.iterator (); while (It.hasnext ()) {Mans 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 types of generic storage limitations:
Ceiling:? Extends E,? You can only accept subclasses of type E or E

Lower:? Super E, accept the parent of type E or e


third, the embodiment of the ceiling:

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"), Al1.add (The New Man ( "DGH", 11)); arraylist<worker> Al2 = new arraylist<worker> (), Al2.add (New Worker ("abc"), Al2.add (New Worker ("dGH"), 11)); Arraylist<student> Al3 = new arraylist<student> () Al3.add (New Student ("abc"), Al3.add (New Student (" dGH ", one)); Al1.addall (AL2);//When the element is generally stored, it is the upper limit, because it is calculated by the upper limit type, does not appear the type security hidden Danger System.out.println (Al1.size ());}}

Look at the API documentation in detail, AddAll method: AddAll (? Extends e), can store all subclasses of E and E


Iv. the embodiment of the lower limit:

The lower bound is usually used when removing an element from a set.
That is, regardless of the type (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 (Mans O1, man O2) {//TODO auto-generated method Stubint t = O1.getname (). Co Mpareto (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"), Al1.add ("DGH", 11)); treeset<worker> Al2 = new treeset<worker> (), Al2.add (New Worker ("abc"), Al2.add (New Worker ("dGH", 11)); Treeset<student> Al3 = new treeset<student> (new Comparaname ()), Al3.add (New Student ("abc"), Al3.add ( New Student ("dGH", 11));//Add the students and workers to the AL1 set Al1.addall (AL3); Al1.addall (AL2);//All sorted according to man's collation iterator<man> it = Al1.iterator (); while (It.hasnext ()) System.out.println (It.next ());}}

Five, the embodiment of the wildcard character

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"), Al1.add (The New Man ( "DGH", 11)); arraylist<man> Al2 = new arraylist<man> () Al2.add (New Man ("ABC"), Al2.add ("DGH"); Boolean Flag1 = Al1.containsall (AL2); System.out.println (FLAG1); Arraylist<string> Al3 = new arraylist<string> () Al3.add ("Qwertyi"); Al3.add ("ASD"); Boolean flag2 = Al1.containsall (AL3); System.out.println (FLAG2);} Method: Containsall (Collection <?> C)//? Regardless of what is received, it is like the Equals method in object: "ASD". Equals (New Worker ("ASD", 12)), compiles and runs through public static void show (COLLECTION<?). > Al) {for (iterator<?> it = Al.iterator (); It.hasnext ();) {System.out.println (It.next ());}}}



Java Learning Lesson 42nd-Generics (ii)-generic interface && wildcard application

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.