[Java learning notes] generic and java learning notes

Source: Internet
Author: User
Tags addall

[Java learning notes] generic and java learning notes
Generic:

Jdk1.5 Security Mechanism

Benefits:

1. Run the ClassCastException problem to the compile time.

2. This avoids the trouble of forced conversion.

 

<>:

When will it be used?

When the referenced data type of the operation is uncertain, use <>. You can pass in the reference data type to be operated.

In fact, <> is a parameter range used to receive specific referenced data types.

 

In a program, as long as a class or interface with <> is used, it is necessary to specify the specific reference data type passed in.

 

 

Generic technology is a technology used by compilers for compilation. Ensures type security.

 

During runtime, the generic type is removed, and the generated class file does not contain the generic type. This is calledGeneric Erasure.

  Why is it erased?To be compatible with the running class loaders.

 

Generic Compensation: During running, the conversion operation is performed by obtaining the element type. You do not need to force the conversion.

 

Wildcard :? Limitations of an unknown type wildcard:

  ? Extends E: Accept the child type objects of the E type or E type. The upper limit is generally used to store objects. For example, add the element addAll boolean addAll (<? Extends E> c)

  ? Super E: Accept E type or E parent type object. Lower limit, which is generally used to retrieve objects. For example, comparator.TreeSet(Comparator<? super E> comparator)

 

Generic example:
1 import java. util. ArrayList; 2 import java. util. Iterator; 3 4 public class GenericDemo {5 6 public static void main (String [] args) {7 ArrayList<String>Al = new ArrayList <String> (); // force convert 8 9 al. add ("abc"); 10 al. add ("hahah"); 11 // al. add (123); // error 12 13 Iterator will be reported<String>It = al. iterator (); // forcibly converts 14 15 while (it. hasNext () 16 {17 String str = it. next (); // do not forcibly convert to 18 System. out. println (str); 19} 20 21} 22 23}

 

Comparator, Comparable generic

1 package p2; 2 3 public class Person implements Comparable<Person>{4 private String name; 5 private int age; 6 7 public Person () {8 super (); 9} 10 11 public Person (String name, int age) 12 {13 super (); 14 this. name = name; 15 this. age = age; 16} 17 18 @ Override 19 public int compareTo (Person o) {20 // Person p = (Person) obj; // you do not need to forcibly convert 21 int temp = this. age-o. age; 22 23 return temp = 0? This. name. compareTo (o. name): temp; 24} 25 26 public String getName () {27 return name; 28} 29 30 public void setName (String name) {31 this. name = name; 32} 33 34 public int getAge () {35 return age; 36} 37 38 public void setAge (int age) {39 this. age = age; 40} 41 42/* (non-Javadoc) 43 * @ see java. lang. object # hashCode () 44 */45 @ Override 46 public int hashCode () {47 final int prime = 31; 48 I Nt result = 1; 49 result = prime * result + age; 50 result = prime * result + (name = null )? 0: name. hashCode (); 51 return result; 52} 53 54/* (non-Javadoc) 55 * @ see java. lang. object # equals (java. lang. object) 56 */57 @ Override 58 public boolean equals (Object obj) {59 if (this = obj) 60 return true; 61 if (obj = null) 62 return false; 63 if (getClass ()! = Obj. getClass () 64 return false; 65 Person other = (Person) obj; 66 if (age! = Other. age) 67 return false; 68 if (name = null) {69 if (other. name! = Null) 70 return false; 71} else if (! Name. equals (other. name) 72 return false; 73 return true; 74} 75 76/* (non-Javadoc) 77 * @ see java. lang. object # toString () 78 */79 @ Override 80 public String toString () {81 return "Person [name =" + name + ", age = "+ age +"] "; 82} 83 84 85 86} 87 88 89 package p3; 90 91 import java. util. comparator; 92 93 import p2.Person; 94 95 public class ComparatorByName implementsComparator <Person>{96 97 @ Override 98 public int compare (Person o1, Person o2) {99 int temp = o1.getName (). compareTo (o2.getName (); 100 101 return temp = 0? O1.getAge ()-o2.getAge (): temp; 102} 103 104 105} 106 107 import java. util. iterator; 108 import java. util. treeSet; 109 110 import p2.Person; 111 import p3.ComparatorByName; 112 113 public class GenericDemo2 {114 115 public static void main (String [] args) {116TreeSet <Person>Ts = newTreeSet <Person>(New ComparatorByName (); 117 118 ts. add (new Person ("sfsf", 21); 119 ts. add (new Person ("MRM", 14); 120 ts. add (new Person ("erw", 18); 121 ts. add (new Person ("iu", 30); 122 123 Iterator<Person>It = ts. iterator (); 124 125 while (it. hasNext () 126 {127 Person p = it. next (); 128 129 System. out. println (p); 130} 131 132} 133 134}

 

Class and method generics

1 public class Tool <QQ> {2 private QQ q; 3 4 public QQ getObject () {5 return q; 6} 7 8 public void setObject (QQ object) {9 this. q = object; 10} 11 12/* 13 * define generics on Methods 14*15 **/16 17 public <W> void show (W str) 18 {19 System. out. println (str. toString (); 20} 21 22 public void print (QQ str) 23 {24 System. out. println (str); // If str is written. length (); is incorrect; because you do not know the specific type of wildcard QQ, you cannot use the specific type of method. 25 // However, you can use the Object Method 26} 27 28/* 29 * when the method is static, the generic type defined on the category class cannot be used. 30 * If the static method uses generics, the generics can only be defined on the method. 31 * The generics must be placed before the return value type, and 32*33 **/34 public static <Y> void method (Y obj) 35 {36 System. out. println (obj); 37} 38 39 40 41}

 

Related Article

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.