Transferred from: http://shuyangyang.blog.51cto.com/1685768/103076
————————————————————————————————————
What is a generic type?
Generics (Generic type or Generics) are an extension of the Java language type system to support the creation of classes that can be parameterized by type . You can think of a type parameter as a placeholder for the type that you specify when you use a parameterized type, as if the form parameter of the method is a placeholder for the value passed at run Time.
The map class allows you to add objects of any class to a map, even if the most common scenario is to save an object of a particular type (such as a String) in a given map (map).
Because Map.get () is defined to return an Object, it is generally necessary to cast the result of Map.get () to the desired type, as shown in the following code:
- Map m = new HashMap ();
- M.put ("key", "value");
- string result = (string) M.get ("key");
In order for the program to compile, the M.get ("key") must be cast to the string type because the string type value is placed here, and the result must be of type String. If you put a value in the map that is not of type string, the classcastexception error will be Reported.
ideally, you might come to the view that M is a map that maps a string key to a string Value. This allows you to eliminate coercion of type conversions in your code and to obtain an additional type-checking layer that prevents someone from saving the key or value of the wrong type in the collection . This is the work that generics do.
Benefits of Generics
Type safety
The type of the variable definition is Restricted.
Eliminate forced type conversions
Or take the example that you just said
- map<string, string> m = new hashmap<string, string> ();
- M.put ("key", "value");
- String result = M.get ("key");
The code above is not cast less and the code looks more concise, which is one of the benefits of Generics. Usually when you write, the editor will have a yellow exclamation point to remind you that you need to use Generics.
wildcard character of generic type
? Wildcard characters
Can I use wildcards to refer to various other parameterized types? a wildcard-defined variable is primarily used as a reference, and you can call methods that are not parameterized, and cannot invoke methods related to Parameterization.
Let's look at an example:
- public class People {
- Private String name;
- Private String job;
- Public people () {
- }
- Public people (string _name, string _job, int _sex) {
- THIS.name = _name;
- This.job = _job;
- This.sex = _sex;
- }
- public void SayHello () {
- SYSTEM.OUT.PRINTLN ("i am a" + job + "; My name is" + name ");
- }
- Public String getName () {
- Return name;
- }
- public void SetName (String Name) {
- THIS.name = name;
- }
- Public String getjob () {
- Return job;
- }
- public void Setjob (String Job) {
- This.job = job;
- }
- public int Getsex () {
- Return sex;
- }
- public void Setsex (int Sex) {
- This.sex = sex;
- }
- private int sex;
- }
- public class Softwareengineer extends people{
- public void Coding () {
- System.out.println ("coding ...");
- }
- }
- Public class Cook extends people {
- public void Cook () {
- System.out.println ("cooking ...");
- }
- }
Main Method:
- list<? Super people> flist = new Arraylist<people> ();
- Flist.add (new People ());
- Softwareengineer soft = (softwareengineer) Flist.get (0);
- Soft.coding ();
Output result: Coding ...
Note that this is super, so add new people () to Flist.add (), or add new Softwareengineer () or new Cook (), either!
well, Let's take a look at the Following:
- list<? Extends people> flist = new Arraylist<people> ();
You can not add the ...?
Why can't I join the subclasses of the people class and the people class in the list because of this:
list<? Extends people> indicates that the upper limit is people, and the following assignments are legal
- list<? Extends people> list1 = new Arraylist<people> ();
- list<? Extends people> list2 = new Arraylist<softwareengineer> ();
- list<? Extends people> list3 = new Arraylist<cook> ();
If list<? Extends people> the method of supporting the Add method is Valid:
List1 can add people and all people subclasses
List2 can add softwareengineer and all Softwareengineer subclasses
List3 can add cook and all Cook's subclasses
In that case, the problem will Arise.
list<? Extends people> should hold the object is the subclass of people, and specifically which subclass is still an unknown, so adding any people subclasses will have a problem, because if add people, maybe list<? Extends people> holds the object is new arraylist<cook> () Softwareengineer The addition is certainly not possible, if add Cook, may list< Extends people> holds a subclass of new Arraylist<cook > ()
Softwareengineer's accession is not legal, so list< Extends people> list cannot be add, and list<? The Super People> list indicates that the list holds an object that is the parent of people, and the lower bound is people, so there is no problem with the subclass of add people or people in the List.
Understanding of Java Generic Collections