-1

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?

Archmede
  • 1,070
  • 1
  • 12
  • 28
cloudiebro
  • 115
  • 2
  • 11
  • 1
    Possible duplicate of [How to create a generic array in Java?](http://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java) – Nemus May 03 '17 at 04:51
  • Yes. A `LinkedList` is a queue. [java.util.LinkedList](https://docs.oracle.com/javase/7/docs/api/java/util/LinkedList.html) – Obicere May 03 '17 at 04:51
  • 2
    @Nemus that hardly applies here. – Obicere May 03 '17 at 04:52
  • 1
    You have multiple things happening here. Generics is one of them. The left hand type `Queue` is an _interface_, or a pattern of behavior which classes implement. The RHS type `LinkedList` is an _implementation_ of a queue. – Tim Biegeleisen May 03 '17 at 04:52
  • ah gotcha thank you @TimBiegeleisen – cloudiebro May 03 '17 at 04:55
  • Yes, that sounds right. Since you defined your list to use `TreeNode` then if later in the code you try to stuff some other type into it, you will get a compiler error. Type safety is one things which generics are there for. – Tim Biegeleisen May 03 '17 at 04:56
  • Possible duplicate of [What are Generics in Java?](https://stackoverflow.com/questions/7815528/what-are-generics-in-java) – JRG Jul 15 '17 at 09:27

4 Answers4

3

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.

user156327
  • 1
  • 4
  • 32
  • 56
Jay Smith
  • 2,107
  • 3
  • 11
  • 26
0

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.

Archmede
  • 1,070
  • 1
  • 12
  • 28
0

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.

Archmede
  • 1,070
  • 1
  • 12
  • 28
Vpn_talent
  • 720
  • 7
  • 20
0

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.

Satish Kumar
  • 110
  • 10