Java Basics-Generics

Source: Internet
Author: User

When generics are not used in the collection:
1. Objects of any object and its subclasses can be added in
2. Remove from class, strong to a certain type, there may be classcastexception exception
1. Use generics in the collection:
Format:list<integer> List = new arratlist<integer> ();
Note:<> can only fill in the reference data type integer, cannot fill in the basic data type int
    map<string, integer> map = new hashmap<string, integer> ();

or map<string, integer> map = new hashmap<> ();
set<map.entry<string, integer>> Set = Map.entryset ();
for (map.entry<string, integer> s:set) {

}

2. customizing Generic class, generic interface, generic method
public class order<t>{all involve the type in T instead, constructor, get Set method, ToString method can have, normal write}
1. When you use (instantiate an object of a generic class, indicate the type of the generic type. Indicates that, at a later time, the location of the generic used in the corresponding class is instantiated as the type of the specified generic)
order<boolean> Order = new order<boolean> ();

2. When instantiating without using , the default is Object type Order order = New Order ();

3. When inheriting a generic class or generic interface, you can indicate the type of the generic
class suborder extends order<customer>{properties and methods are all types of Customer}
can also not specify:
class Suborder<t> extends order<t>{properties and methods are all types of T}
4. Declaring generics Method: public <E> e Gete (e e) {return E;}

Public <E> list<e> copytolist (E-E, list<e> List) {for    (e e1:e) {        list.add (e1);    }    return list;}    

5. When invoking a generic method through an object, indicate the type of the generic method
   Integer i = Order.gete (23°c);
3. The relationship between generics and inheritance
Class A is a subclass of class B, but list<a> is not a list<b> sub-interface, which is a side-by-side relationship

List<object> list1 = null; list<string> list2 = new Arraylist<string> () List1 = list2;//Type mismatch (this cannot be done)

4. Wildcard characters?
Class A is a subclass of class B, and G is a class or interface with a generic declaration, so G<?> is the parent of g<a>, G<b>

List<?> is list<a>, List<b> The parent class list<?> list = null; List<object> List1 = new arraylist<object> (); list<string> list2 = new arraylist<string> (); list = List1;list = list2;//this is possible.

? Extends a: Can hold subclasses of a and a
? Super A: Can store a and its parent class

Note: You are not allowed to write to a collection that is declared as a wildcard, but you can read objects that are declared as wildcard characters in a collection class

iterator<?> ITERAOTR = List.iterator (); while (Iterator.hasnext ()) {    System.out.println (Iterator.next ()); /This is object's}

List.add ("AA") is not allowed.
However, you can save null list.add (NULL);


5. The generic type of the class cannot be used in a static method.
6. If the generic class is an interface or abstract class, you cannot create an object of a generic class.
7. Cannot use generics in catch
8. Deriving subclasses from generic classes, generic types need to be materialized

Java Basics-Generics

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.