The generic type in a Java set refers to the object type stored in the set at the same time when the set is created. This is mainly based on the type security considerations.
For example, if we want to create an ArrayList for storing strings, we usually use the following method:
ArrayList<String> list = new ArrayList<String>();
Note the angle brackets and their content, so that we can only store the String type objects in this list. When we try to add non-String type objects to this list (reference) when the compilation fails, the system prompts "the symbol cannot be found" (I think the prompt "The method cannot be found" is more accurate ). The reason is that we have declared the String type stored in the ArrayList. When we add an object (reference) to this ArrayList, the compiler will check whether the object is of the String type. If not, compilation fails. This is a so-called type security issue.
When the object (reference) is stored in the array, the compiler will also check. When other types of objects are added, the compilation will not pass, prompting "incompatible types ".
The above is the generic problem in the set. Let's take a look at the polymorphism problem in the set below, which is the core of this article.
We know that polymorphism in Java means that the type of a reference variable defined in a program and the called method cannot be determined during encoding, it can only be determined during running. Simply put, the reference variable you define is of the parent type, and the object you create is of the Child type. Use the reference variable of the parent type to point to the object of the Child type, then, when you call the method of this object, whether the method of the parent class is called or the method of the subclass can be determined only during the operation, it is possible to call the method of the parent class or the method of the subclass that may be called, so that you can select multiple running states without modifying the Code. This is polymorphism. (For polymorphism, check the book if you do not understand it .)
With regard to polymorphism, a simple understanding is suitable for the parent class and must also be suitable for subclasses. That is, the place where the parent class exists can also be replaced by subclasses (and this actually corresponds to a design principle, you also know, right, that is, the Lee's replacement principle. I will not introduce this principle here, but by the way .)
Based on this, we consider the situation of polymorphism in the set.
Assume that there is a method as follows (Animal is a class, and Dog is a subclass ):
public void method(Animal anim){}
According to polymorphism, we know that we can replace the parent class with subclasses, while Dog is a subclass of Animal, that is, we can use a Dog type object as the parameter of this method. This is no problem. No problem. Let's continue.
Let's introduce the set and generic types. assume there is a method as follows:
public void method(ArrayList<Animal> anim){}
Then, according to polymorphism, We can theoretically pass the following parameter: An ArrayList <Dog> type object (the Dog is a subclass of Animal). It's Okay theoretically, but it's actually quite a problem, compilation fails.
Why? Dog is a subclass of Animal. Where the parent class exists, the subclass can exist. This should be correct. This is indeed true, and the error is not a polymorphism. Let's look at it in a reverse way. If the above situation is allowed, you pass in the ArrayList <Dog> object where an ArrayList <Animal> object is required, however, you cannot predict whether the next situation will happen, that is, you call anim in method. add (new Cat (), which is understandable (assuming Cat is another subclass of Animal), because the parameter type of the method is of the ArrayList <Animal> type, that is, the ArrayList stores Animal, and Cat is a subclass of Animal, so you can call anim. add (new Cat (), but in fact the ArrayList <Dog> type you pass is true, that is, you store other objects to an ArrayList that stores dogs, this is not allowed based on generic security issues. Therefore, if this is not true, you cannot pass the ArrayList <Dog> type object to an ArrayList <Animal> method. (The real TM image uses the reverse identification method to prove the mathematical problem. You can find it !)
Dizzy, I hope you do not.