What is usage? in java

I would like to learn about usage? in java generics. Having examined the T placeholder and the template ?, I wondered ?, I went through several websites / pages and books, but did not understand this. So I created the class below to explore the differences.

import java.util.List; public class Generics2 { public <T> void method1(List<T> list){ System.out.println(list); } public <T extends Number> void method2(List<T> list){ System.out.println(list); } /*public <T super Integer> void method3(List<T> list){ }*///super does not work. public void method4(List<?> list){ System.out.println(list); } public void method5(List<? extends Number> list){ System.out.println(list); } public void method6(List<? super Integer> list){ System.out.println(list); } public <T> void copy1(List<T> list1, List<T> list2){ //copy elements from list1 to list2 }//It does not span well with copy of one type of elements from list1 to other type elements in list2, where the list elements //between the two are not same but are related through inheritance. public <T1,T2> void copy2(List<T1> list1,List<T2> list2){ //copy elements from list1 to list2, right now we do not bother about the exceptions or errors that might generate. }//Our intention here is not to copy elements with relation between T1 and T2. We intend to explore the differences on T and ? public void copy3(List<?> list1,List<?> list2){ //copy elements from list1 to list2, right now we do not bother about the exceptions or errors that might generate. }//Our intention here is not to copy elements with relation between T1 and T2. We intend to explore the differences on T public <T1 extends Object, T2 extends Object> void copy4(List<T1> list1, List<T2> list2){ //copy elements from list1 to list2 } public static void main(String[] args) { // TODO Auto-generated method stub } } 

Here in one case there may be several scripts that are not in my class, therefore, what I wrote is incomplete, in this case, someone can help me implement more scripts. Or did I find? as redundant, in addition to providing functions such as using the super keyword and the smaller characters in the method signature along with the return type.

EDIT: Basically, my question is to know the reasons associated with the implementation? wildcard where a generic type can replace it everywhere. It's not about how to use? or T. Of course, knowing that using it will provide some users. For example, the things that I deduced:

  • ? makes the code more readable and less easy to code to specific places
  • Sometimes this sometimes leads to bloat code.
  • We can use superclasses, which is not possible when using type T.
  • Limits the addition of new random items to the list. If casting (without classcastexception) is valid for type T sometimes.

Is there any more?

+6
source share
3 answers

You use unlimited wildcards when a list (or collection) has unknown types.

You should never use it if you want, for example, inserting values ​​into a list (since the only value you can insert is null ).

It is used when you want to get information about a data structure, for example, about printing its contents, when you do not know what type it may contain. For instance:

 public static void printList(List<?> list) { for (Object elem: list) System.out.print(elem + " "); System.out.println(); } 
+2
source

? means: type not specified. Meaning: if there is no other reason in the code that requires a parameter of type "named"; can you / should you use ? to express just that.

And actually, I asked some time ago so you can find the answers here: Java generics: wildcard <? > vs type <E>?

+3
source

Someone wrote a good answer to this question here . The question icon basically means "any type", but it can be very useful when working with interfaces or abstract classes. For instance,

 List<? extends MyAbstract> myClasses = new ArrayList<? extends MyAbstract>(); 

This is useful because you still get type checking to make sure everything on your list is a subclass of MyClass. If you had a specific method or field in an abstract class that you needed to work with, you can work with it for all subclasses, regardless of the particular implementation. For a more real-world example, I use this to route URLs:

 Map<String, Class<? extends IJSONService>> routes = new HashMap<String, Class<? extends IJSONService>>(){ { put("default", EmployeeFinderSearch.class); put("", EmployeeFinderSearch.class); put("reports", EmployeeFinderReports.class); put("me", Me.class); put("email", SendEmail.class); put("image", UploadImage.class); put("excel", ExportExcel.class); }}; 
+1
source

Source: https://habr.com/ru/post/987416/


All Articles