Removing from the queue will be from the first so the same thing happened with the queue technique over in java as well they maintain the same thing when you insert something to the queue it will add that thing in the last and when you remove something from the queue it will remove from the beginning right so this is the thing I said cue insert the elements in the end and removes from the beginning right here does not allow any null operation and that will not support any null operation.
You really need to understand that q does not allow any null operation okay and q has q has two interfaces one is linked list one is priority key but you guys have a doubt that what is the difference between q and priority q let me tell you what is the difference between q and priority so q uh for the priority for the queue q is where is the insertion is done at one end and removal is done from the other end right okay but from the priority queue what priority queue does it other elements can be inserted in any order but the removal of the element will be in a sorted manner right so for that if you want to have a sorted queue we use a priority queue for that sometime right so this is the basic difference between a queue and priority queue so let me show you how can you implement q with using collection framework.
First, we need to import the full package we have then we need to initialize the queue how can we initialize we can call q or we can use priority queue I will show you how can you call the q and how can you use the priority queue right so how can you initialize them both okay now comes to the elements how can you add an element we use dot add a method to add the element in the queue right and if you want to display the queue the normal thing do like write system. out. println then pass your q name right now comes the predefined function.
We have in q so what are the predefined function we have we have dot peak what is dot p dot peak will return the first element of your q, okay and when you use the remove function I told you it uses the FIFO uh technique when you remove when you use a remove function it will remove the first element okay the first element from your queue right if you want to see the length of the cube we use dot size so let’s see and let’s go to eclipse and let’s see how can we implement cube in eclipse using java so we came to eclipse okay so already I have uh made a class for the queue you all know how can you make a class where I did uh I actually called uh initialize the queue using priority queue you can see this is using priority queue then what I did I actually.
Now we will see how can we uh implement the queue using eclipse in java right so what I did I actually called a class that is q demo now I actually initialize the queue right I first write priority queue then the type of the queue that is a string then the name of that you want to have that is I want the queue normal then-new priority queue then the what type of like you want that already I have specified this is the option if you want you can put otherwise you can skip it again I use to stream and then the first bracket, okay and after that, I use dot add a method to add elements to add data to our cube that is orange first I add then I add apple then I add crepes then I add lychee maybe I’m gonna add one more that is q dot add I am gonna go to add banana okay so yeah so I have added right now if I want to see that whatever the data I have added that is added properly in the queue or not.
Let me check it out uh let me run it basically yeah you can see apple lychee grapes orange and banana is there but it’s not it does not maintain the insertion order the way we insert because I told you that priority queue uses the sorted manner you can see we have a l g o bright we uh we came to eclipse now we are going to make a class I make a class for it that is q demo right now what I need I actually call the priority queue called a priority queue.
You need to specify what type of a queue you want that is string queue I want then I add cue the name of the queue you are queue you want to put that is I want a queue name that’s normal queue then I use new priority queue then again what type of a queue you want and this is the option if you want to put you can otherwise you can uh make it null as well so I again use a string and then the bracket right this is how we initialize q then how can we add an element to the queue so dot add method we used in the dotted method uh in the dot hat method I passed the data that is first is orange apple grapes lychee and banana.
We actually give those data to the dot 8 methods no to see that our queue is gonna is taking that element or not let me execute the code for you guys yeah you can see uh it takes and it stores all the data that is apple lychee grapes orange and banana okay so these are the uh these this is the main uh method we can say dot added method which actually helps us to add something to our queue right now we will see that how can we use like how can you get the first element from the cube right so let me uh just try to print that system sorry dot out dot print ln and in that we will use q dot you can see these are the option we have so dot peak I’m gonna use let’s see what will happen with that what is going to print right you can see the apple it will give you the first element okay.
It will give you the first element of the queue that is how peak works okay now we will see some other uh technique as well let’s see uh there is one more uh method we have that is system uh thought out or cream if you use dot element that also will give you the first element of the queue right q dot element let’s see what is going to give see for element and peak both are going to give you the first element of the queue right if you want to use remove method if i want to it should remove the apple right the first element of the queue it should remove now i am going to print the queue you can see that using remove what it removes the first element so it is perfectly using fifo uh fifo technique right this is all we really need to know about queueing we talk about q right so these are the basic function again guys i am talking about that these are the basic function you really need to know you need to understand if you want to manipulate a queue that time you need to understand that okay.