Understanding generics in Java

限于喜欢 提交于 2021-02-04 21:55:06

问题


Can someone explain to me what this code is doing, I am new to java.

Queue <TreeNode> queue = new LinkedList<TreeNode>();

I believe this code creates a Queue with the variable name queue that is of type TreeNode, what does the right side mean? Is it assigning the Queue to be a Linked List of type TreeNode?


回答1:


Class java.util.Queue uses type parameters to assure that you won't add type other than it was created with.

If you create Queue like:

Queue <TreeNode> queue = new LinkedList<TreeNode>();

It means queue can store only TreeNode objects. This is used for type checking in methods add,offer,remove,poll.




回答2:


Yes to understand this you must have at least little understanding of generics in java. The generics are very important feature of java and java collection framework use this feature in full form. Let me describe your statement given below.

Queue <TreeNode> queue = new LinkedList<TreeNode>();

Here First thing to be notice is that almost all collection classes and interface are generic in nature. That means they can store any type of data in them. See the below example.

List list = new ArrayList();

    String sObj = "stringObj";
    Integer iObj = 1;
    MainTest classObj = new MainTest();

    list.add(sObj); //Storing string 
    list.add(iObj); // Stroing int 
    list.add(classObj); //Storing class object

Here you can see that i have created a ArrayList and referenced it through a List reference variable. Here you can notice this i have not metioned any class here so by default it will store all object types of objects. Now if we come to your code that means you are restricting the collection to store only one type of objects which is TreeaNode .So that any other type of object will be added in this collection any more. In you statement in left side you are Creating a reference variable with name queue whose data type is Queue of TreeNode while on right side you are assigning a object of LinkedList which can store only TreeNode objects int to you queue reference variable.That means you queue will not be allowed to add any object in it excluding TreeNode. This assignment is possible both classes Queue and LinkedList implements Collection interface.




回答3:


The link below describes what a generic is in Java and why they are used.

https://docs.oracle.com/javase/tutorial/java/generics/why.html

By Using generics we make sure the type safety of our objects.

In your question

Queue <TreeNode> queue = new LinkedList<TreeNode>();

you have created queue with generic type "TreeNode". So it'll allow only those objects which will instanceOf "TreeNode" class.




回答4:


A Generic allows you to specify what type of reference a DataStructure will store. In your example the List will variables of type TreeNode.

But you can store any type, including your own user defined types such as classes.



来源:https://stackoverflow.com/questions/43751304/understanding-generics-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!