This is the 7th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.
The vast sea of millions, thank you for this second you see here. I hope my article is helpful to you!
May you keep your love and go to the mountains and seas in the coming days!
Foreword: Yesterday we finally covered the basics of using generic wildcards,
So, next we’ll look at generic wildcards — advanced use of generic wildcards!
👺 Generic wildcard
In our previous learning about generics, when we used a generic class or interface, we passed data in which the generic type was uncertain. We could use the wildcard <? > said. But once you use generic wildcards, you can only use the generic methods of the Object class, not the elements in the collection themselves.
😟5.2 Advanced use of wildcards
When you set generics, you can actually set them as long as they’re classes. But in JAVA generics you can specify the upper and lower limits of a generics.
Upper limits on generics:
- format:
Type name <? Extends class > Object name
- meaning:
Only this type and its subclasses can be accepted
The lower limit of generics:
- format:
Type name <? Super class > object name
- meaning:
Only this type and its parent type can be accepted
For example, we now know the class Object, Animal, Dog and Cat, of which Animal is the parent of Dog and Cat
Class Dog extends Animal{} class Dog extends Animal{Copy the code
-
First let’s look at the upper bound of generics
:// ArrayList<? extends Animal> list = new ArrayList<Object>(); / / error ArrayList <? extends Animal> list2 = new ArrayList<Animal>(); ArrayList<? extends Animal> list3 = new ArrayList<Dog>(); ArrayList<? extends Animal> list4 = new ArrayList<Cat>();Copy the code
As you can see, the upper limit of a generic type can only be the type of that type and its subclasses.
-
Let’s look at the lower bound of generics
:ArrayList<? super Animal> list5 = new ArrayList<Object>(); ArrayList<? super Animal> list6 = new ArrayList<Animal>(); // ArrayList<? super Animal> list7 = new ArrayList<Dog>(); // ArrayList<? super Animal> list8 = new ArrayList<Cat>(); / / an errorCopy the code
As you can see, the lower limit of a generic can only be the type of that type and its parent class.
-
The upper and lower bounds of generic types are also used for passing parameters:
For example: now we know the Object class, String class, Number class, Integer class, where Number is the parent of Integer class
public static void main(String[] args) { Collection<Integer> list1 = new ArrayList<Integer>(); Collection<String> list2 = new ArrayList<String>(); Collection<Number> list3 = new ArrayList<Number>(); Collection<Object> list4 = new ArrayList<Object>(); getElement(list1); getElement(list2); / / error getElement (list3); getElement(list4); / / error getElement2 (list1); / / error getElement2 (list2); / / error getElement2 (list3); getElement2(list4); } // The upper limit of generics: generics at this time? Public static void getElement1(Collection<? Extends Number> coll){} extends Number> coll){} Public static void getElement2(Collection<? super Number> coll){}Copy the code
We are done with generics!
🌸 summary
I believe that you have all the in-depth understanding and experience of generics, in peacetime development, more common use in the use of generic framework List and Map. Of course, there are a lot of applications, look forward to you slowly discover!
Well, let’s keep looking forward to the next chapter!
Let’s refuel together, too! I am not just, if there is any missing, wrong place, also welcome you to criticize in the comments of talent leaders! Of course, if this article is sure to help you a little, please kindly and lovely talent leaders to give a thumb-up, favorites, one key three even, thank you very much!
Here, the world is closed for today, good night! Although this article is over, I am still here, never finished. I will try to keep writing articles. The coming days are long, why fear the car yao ma slow!
Thank you all for seeing this! May you live up to your youth and have no regrets!