Java Collection Framework---generic summary

Source: Internet
Author: User

/* Generics: Specify the collection type to identify problems at run time rather than compile time, eliminating security risks. Avoid strong turns.  */package pack;import Java.util.arraylist;import Java.util.iterator;/*public class Main {public static void Sys (Object    obj) {System.out.println (obj); } public static void Main (string[] args) {treeset<string> Al = new Treeset<string> (New Mycomparator  ());        Set generic Al.add ("AA");        Al.add ("BBB");        Al.add ("C");        Iterator<string> it = Al.iterator ();  while (It.hasnext ()) {String s = (string) it.next ();            Avoid strong-turn SYS (s);        SYS (It.next ()); }}}class Mycomparator implements comparator<string> {public int compare (String o1,string O2) {int a        = new Integer (O2.length ()). CompareTo (New Integer (O1.length ()));        if (a==0) {return O2.compareto (O1);    } return A; }}*//*public class Main {public static void Main (string[] args) {demo<integer> d = new DEMO&LT;INTEGER&G        t; (); D.show (4);        D.show (New Integer (5));        demo<string> D1 = new demo<string> ();    D1.show ("haha");    }}class demo<t> {//generic method, currently undefined what type public void Show (T t) {System.out.println (t); }}*//*interface inter<t> {//generic interface void Show (T-t);}    Class Demo<t> implements inter<t> {public void show (T t) {System.out.println (t); }}public class Main {public static void Main (string[] args) {demo<integer> d = new demo<integer> ()        ;    D.show (New Integer (3)); }}*/public class Main {public static void Main (string[] args) {arraylist<string> al1 = new arraylist<        String> ();        Al1.add ("JAVA1");        Al1.add ("Java2");        Al1.add ("Java3");        arraylist<integer> Al2 = new arraylist<integer> ();        Al2.add (1);        Al2.add (2);        Al2.add (3);        Show (AL1);    Show (AL2); } public static <T> void Show (Arraylist<t> a) {//t can be used? Replace       Iterator<t> it = A.iterator ();        while (It.hasnext ()) {System.out.println (It.next ()); }    }}

Java Collection Framework---generic summary

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.