1 00:00:01,150 --> 00:00:06,910 ‫In this video, we are going to cover cues and we're going to see how to use queues in our applications. 2 00:00:07,180 --> 00:00:13,240 ‫Queues are significant and used a lot by our operating system, for example, to handle tasks, scheduling 3 00:00:13,240 --> 00:00:14,920 ‫and many other processes. 4 00:00:15,100 --> 00:00:21,940 ‫Queues should be used when the order of the data is important, as we will see in our example. 5 00:00:22,030 --> 00:00:26,410 ‫So let's go ahead and first of all, define a queue. 6 00:00:26,410 --> 00:00:30,640 ‫In order to define a queue, you can use the queue. 7 00:00:30,670 --> 00:00:36,910 ‫Keyword queues are also part of the generic collections, which means we need to define what type a 8 00:00:36,910 --> 00:00:39,220 ‫queue will incorporate. 9 00:00:39,220 --> 00:00:44,140 ‫So in this case it will hold a bunch of integers, but it could hold any other type as well. 10 00:00:44,140 --> 00:00:47,620 ‫It could hold strings, it could hold even a whole objects. 11 00:00:47,620 --> 00:00:52,450 ‫So you are fully free to define whatever you want this item to hold. 12 00:00:52,450 --> 00:00:59,140 ‫So queue then less, then sign into greater sign and then the name of the queue equals. 13 00:00:59,140 --> 00:01:05,890 ‫And then you can initialize it straight away as we do here, where we just create an empty queue that 14 00:01:05,890 --> 00:01:06,970 ‫holds integers. 15 00:01:07,780 --> 00:01:11,260 ‫So now we can go ahead and add items to this queue. 16 00:01:11,260 --> 00:01:15,850 ‫In order to add items to a queue, you can use the end queue method. 17 00:01:15,880 --> 00:01:20,080 ‫The queue method adds an object to the end of the queue. 18 00:01:20,470 --> 00:01:26,440 ‫So if I add an item like one, for example, this item will be the first one because well, it's the 19 00:01:26,440 --> 00:01:29,530 ‫first one that we include, but it will also be the last one. 20 00:01:30,190 --> 00:01:34,990 ‫So as soon as we add another one, the new item will be the last item in the queue. 21 00:01:35,320 --> 00:01:37,000 ‫So that's how ordering is done. 22 00:01:37,090 --> 00:01:42,550 ‫It's similar to a stack, but the difference is that a queue when you get rid of something, so when 23 00:01:42,550 --> 00:01:46,780 ‫you pop an item, it's going to take it from the beginning of the queue. 24 00:01:46,780 --> 00:01:53,890 ‫So the item that was created first will be taken away or will be popped out of the queue, so to speak. 25 00:01:54,610 --> 00:02:03,760 ‫So in order to look at the queues items, we can use the peak method and this peak method will display 26 00:02:03,760 --> 00:02:07,810 ‫the first item in the queue, so the front of the queue, so to speak. 27 00:02:07,810 --> 00:02:14,320 ‫So it's like a queue of people where you're at the counter and you want to order something, for example, 28 00:02:14,320 --> 00:02:19,780 ‫then the peak method will give you the first person in that queue that is about to order. 29 00:02:20,530 --> 00:02:24,040 ‫So let's run this real quick and see how this is going to look like. 30 00:02:24,040 --> 00:02:26,470 ‫And we see the value at the front of the queue is one. 31 00:02:27,820 --> 00:02:32,710 ‫Now let's add a couple more values to see how that's going to affect our application. 32 00:02:32,710 --> 00:02:41,710 ‫So here I'm going to add the different Q items like so I'm going to end Q one then in Q2 and Q3, and 33 00:02:41,710 --> 00:02:43,690 ‫I'm just using these numbers one, two, three. 34 00:02:43,690 --> 00:02:45,760 ‫There could be any other numbers as well. 35 00:02:45,760 --> 00:02:47,680 ‫It doesn't really have to be one, two, three. 36 00:02:48,370 --> 00:02:55,000 ‫So with peaking, we get the item that is at the beginning of the queue. 37 00:02:55,030 --> 00:03:01,750 ‫You can see every single time, no matter what we add to the Q, the one is going to be our first item 38 00:03:01,750 --> 00:03:05,530 ‫in the Q, so it's going to be the one displayed when we use the peak method. 39 00:03:07,650 --> 00:03:14,030 ‫Now in order to get rid of an item from the queue, you would use the queue method. 40 00:03:14,460 --> 00:03:15,360 ‫So here. 41 00:03:15,540 --> 00:03:25,530 ‫Q dot rd q so this D q will now get rid of the first integer of our Q and it will also store it straight 42 00:03:25,530 --> 00:03:33,810 ‫away so we could store this let's call this one Q item, for example, and it will then store the result 43 00:03:33,810 --> 00:03:36,000 ‫of this dict item. 44 00:03:36,000 --> 00:03:41,010 ‫So basically it will store one in there because that's the first item that we have in the list. 45 00:03:41,010 --> 00:03:46,680 ‫And then Peking will display two because now one is not there anymore. 46 00:03:46,710 --> 00:03:47,970 ‫The first item is gone. 47 00:03:47,970 --> 00:03:54,810 ‫So now the value in the Q will be two because well, we just decode the first item. 48 00:03:54,810 --> 00:04:01,500 ‫So it's similar to pop in a stack, but it takes away the item at the beginning of the Q instead of 49 00:04:01,500 --> 00:04:05,790 ‫the end of the Q or the end of the stack, however you want to look at it. 50 00:04:07,180 --> 00:04:11,470 ‫You can, of course also try to queue an item at the beginning. 51 00:04:11,620 --> 00:04:17,420 ‫If there are no elements inside of the queue, but you will run into an invalid operation exception. 52 00:04:17,440 --> 00:04:25,630 ‫The queue is empty so you will need to check if the queue is empty or not before running this. 53 00:04:25,630 --> 00:04:31,240 ‫So here we can run the queue method because we know that we just encode something. 54 00:04:31,390 --> 00:04:38,230 ‫But otherwise you would use the count property of your queue in order to figure out how many items you 55 00:04:38,230 --> 00:04:38,830 ‫have there. 56 00:04:38,830 --> 00:04:43,600 ‫So this is the property that you can use, gets the number of elements contained in the queue and you 57 00:04:43,600 --> 00:04:48,520 ‫can then check if that one is greater than zero, for example, to check if there is anything in the 58 00:04:48,520 --> 00:04:52,120 ‫queue that you can, for example, the queue. 59 00:04:53,050 --> 00:04:59,110 ‫So in this particular example, I'm just going to go ahead and use my. 60 00:04:59,820 --> 00:05:03,000 ‫Do you count to double check in a while loop? 61 00:05:03,120 --> 00:05:11,400 ‫And for every single item I'm going to queue it and also write down the amount of items that are still 62 00:05:11,400 --> 00:05:14,180 ‫in the queue and also the item that was removed. 63 00:05:14,190 --> 00:05:20,580 ‫So this line here will remove the item by calling the queue method and at the same time it will display 64 00:05:20,580 --> 00:05:24,180 ‫the item right here because that's what we're using here. 65 00:05:24,180 --> 00:05:25,710 ‫We're using the string, right? 66 00:05:25,710 --> 00:05:31,920 ‫And with this position zero, it will display whatever is the result of the queue call. 67 00:05:31,920 --> 00:05:35,310 ‫And as you can see here, the queue method will return an integer. 68 00:05:35,310 --> 00:05:37,230 ‫So we are going to display a number here. 69 00:05:37,980 --> 00:05:38,190 ‫Okay. 70 00:05:38,190 --> 00:05:43,260 ‫So let me get rid of this cue that I used here in order to demonstrate how to recue an item. 71 00:05:43,260 --> 00:05:44,730 ‫And let's run it again. 72 00:05:44,730 --> 00:05:46,920 ‫And you can see the value at the front, this one. 73 00:05:46,920 --> 00:05:51,070 ‫And then we basically start the Y loop here. 74 00:05:51,090 --> 00:05:52,290 ‫That's the interesting part. 75 00:05:52,470 --> 00:05:55,270 ‫So one word is removed from the queue. 76 00:05:55,290 --> 00:05:58,520 ‫Now we only have two items left to was removed from the queue. 77 00:05:58,530 --> 00:06:02,010 ‫Now we only have one item left and three wasn't removed from the queue. 78 00:06:02,040 --> 00:06:03,960 ‫Now we only have zero items left. 79 00:06:03,960 --> 00:06:07,050 ‫So basically the queue is empty at that point. 80 00:06:09,030 --> 00:06:09,330 ‫All right. 81 00:06:09,330 --> 00:06:12,400 ‫So let's look at a more or less real world example. 82 00:06:12,420 --> 00:06:19,140 ‫Of course, we don't work with databases in this simple example here, but let's say we have an e-commerce 83 00:06:19,140 --> 00:06:23,550 ‫platform and we're the seller and we receive a bunch of orders. 84 00:06:23,580 --> 00:06:27,390 ‫These orders should be processed so that the server's time. 85 00:06:27,390 --> 00:06:31,470 ‫So the waiting time for the customer should be held to a minimum. 86 00:06:32,010 --> 00:06:36,930 ‫This is why we are going to use queues because they're very practical since we care about the order 87 00:06:36,930 --> 00:06:39,720 ‫of the orders coming in. 88 00:06:39,720 --> 00:06:47,580 ‫So the order of orders, it's a little of a tongue twister here, but the order of which the data was 89 00:06:47,580 --> 00:06:52,110 ‫received and by data I mean the orders themselves. 90 00:06:52,110 --> 00:06:57,180 ‫So let's look at this very simple class that we're going to build here, and I'm going to put it inside 91 00:06:57,180 --> 00:06:58,620 ‫the same file here. 92 00:06:58,620 --> 00:07:05,490 ‫This class order with an order ID property, the order quantity property, and then the constructor, 93 00:07:05,490 --> 00:07:10,860 ‫which basically just sets those two values when we create an order object and then we have this print 94 00:07:10,860 --> 00:07:18,900 ‫message or this process order method which will print the order ID of the item that we're currently 95 00:07:18,900 --> 00:07:22,050 ‫looking at, so of the order that we are currently looking at. 96 00:07:23,040 --> 00:07:30,600 ‫So now what I would like to do is I would like to actually create methods inside of my program class. 97 00:07:30,600 --> 00:07:37,290 ‫So I'm going to put it in here inside of this bracket still and the methods they are going to take care 98 00:07:37,290 --> 00:07:40,080 ‫of orders from the different branches. 99 00:07:40,080 --> 00:07:44,130 ‫So let's say we have two different branches where we can get orders from. 100 00:07:44,130 --> 00:07:49,800 ‫Let's say we are selling on Amazon and at the same time at Best Buy, for example. 101 00:07:49,800 --> 00:07:52,800 ‫And then we will now get those orders. 102 00:07:52,890 --> 00:07:58,830 ‫Now we're not going to make it too complicated where we are going to check which branch is, well, 103 00:07:58,830 --> 00:08:00,540 ‫the order of the different branches. 104 00:08:00,540 --> 00:08:04,650 ‫This is something you could, of course, see as a little challenge for yourself if you want to develop 105 00:08:04,650 --> 00:08:05,010 ‫that. 106 00:08:05,010 --> 00:08:09,960 ‫But we're just going to say, okay, give me the orders from Branch one in the right order and give 107 00:08:09,960 --> 00:08:13,320 ‫me the orders from Branch two in the right order as well. 108 00:08:13,860 --> 00:08:14,320 ‫Okay. 109 00:08:14,400 --> 00:08:21,930 ‫So here this will be Branch one with a bunch of orders and then we have the branch two with a bunch 110 00:08:21,930 --> 00:08:22,440 ‫of orders. 111 00:08:22,440 --> 00:08:23,850 ‫So we're getting the data. 112 00:08:23,850 --> 00:08:29,130 ‫Let's say we get this data from a database where we are checking, okay, what were the orders? 113 00:08:29,130 --> 00:08:31,110 ‫And we would probably check them by date. 114 00:08:31,110 --> 00:08:33,990 ‫So each order would get a date as well. 115 00:08:34,110 --> 00:08:42,750 ‫Here we have the order ID, but usually we would also try to store when it was ordered and so forth. 116 00:08:42,750 --> 00:08:45,090 ‫But we're going to keep it simple for this example. 117 00:08:46,170 --> 00:08:49,770 ‫So we have now an array of orders in both of those examples. 118 00:08:49,770 --> 00:08:58,020 ‫And when we call the method, receive orders from branch one and two, they will basically just re turn 119 00:08:58,020 --> 00:08:59,370 ‫and order array. 120 00:08:59,370 --> 00:09:02,220 ‫So here an array of orders. 121 00:09:03,270 --> 00:09:06,630 ‫So that's what this statement here does, return orders. 122 00:09:09,080 --> 00:09:16,310 ‫Now in our particular example, the order ID is in fact going to be unique for the two different orders. 123 00:09:16,310 --> 00:09:20,540 ‫So they will already be in the right order, so to speak. 124 00:09:20,810 --> 00:09:23,620 ‫So this is order one, this is order two. 125 00:09:23,630 --> 00:09:27,620 ‫Then we have order three and order four and five from branch two. 126 00:09:27,620 --> 00:09:34,310 ‫And then in the evening, in Branch one, there came another order and somebody purchased ten of our 127 00:09:34,310 --> 00:09:35,000 ‫items. 128 00:09:37,420 --> 00:09:44,470 ‫Now in our main method we can create a queue and let me actually get rid of all of this stuff here. 129 00:09:44,800 --> 00:09:48,790 ‫In our main method, we can now go ahead and create a queue of orders. 130 00:09:49,210 --> 00:09:51,040 ‫So here queue order. 131 00:09:51,040 --> 00:09:55,720 ‫You can see that this is not going to be a simple type like integer now, but it's actually going to 132 00:09:55,750 --> 00:09:57,560 ‫hold a bunch of orders. 133 00:09:57,580 --> 00:10:00,480 ‫That's the cool thing about the queue. 134 00:10:00,490 --> 00:10:07,720 ‫So it's not just simple types and I'm going to call this one order queue and I'm going to initialize 135 00:10:07,720 --> 00:10:09,250 ‫it straight away right here. 136 00:10:09,610 --> 00:10:18,310 ‫And then at this point, we can go ahead and and queue all of our orders that we get from Branch one 137 00:10:18,310 --> 00:10:21,130 ‫by using this for loop here, this for each loop. 138 00:10:21,220 --> 00:10:24,310 ‫So we're going through every single order that I'm going to call. 139 00:10:24,310 --> 00:10:30,250 ‫Oh, in this for each loop and this iteration in our receive orders from branch one. 140 00:10:30,490 --> 00:10:35,320 ‫Now this here will be of type order array. 141 00:10:35,770 --> 00:10:40,960 ‫So when we call this method, it will return an array of orders as you see here. 142 00:10:41,230 --> 00:10:49,510 ‫So basically this for each loop will go through the orders that we have inside of this order array and 143 00:10:49,570 --> 00:10:52,150 ‫it will look through every single one of them. 144 00:10:52,150 --> 00:10:57,130 ‫So all will be first this order, then it will be this order, then it will be that order. 145 00:10:57,370 --> 00:11:01,750 ‫And for every one of them it's going to end queue those orders. 146 00:11:03,280 --> 00:11:05,380 ‫So that's what this for each loop does. 147 00:11:05,380 --> 00:11:10,270 ‫And then we create another for each loop for the other branch as well. 148 00:11:10,270 --> 00:11:11,050 ‫So. 149 00:11:12,500 --> 00:11:17,090 ‫Same goes here for each loop or in orders received from branch two. 150 00:11:17,360 --> 00:11:18,450 ‫So this is Branch two. 151 00:11:18,470 --> 00:11:19,700 ‫This is branch one. 152 00:11:20,390 --> 00:11:22,100 ‫And the same applies. 153 00:11:22,100 --> 00:11:23,900 ‫So the same concept applies. 154 00:11:24,140 --> 00:11:31,820 ‫So to process the orders, we will go through our queue one by one, and then we will remove the elements 155 00:11:31,820 --> 00:11:38,300 ‫from our queue and process them until our queue is empty and all orders are finally processed. 156 00:11:38,930 --> 00:11:44,150 ‫That's why we have this process order method here, which will process the orders. 157 00:11:45,260 --> 00:11:52,010 ‫So now we can go ahead and instead of our main method up here, once we have the orders queue with all 158 00:11:52,010 --> 00:11:59,870 ‫of the items in it, we can go through it and we can check if the order queue is not empty and as long 159 00:11:59,870 --> 00:12:00,770 ‫as it's not empty. 160 00:12:00,770 --> 00:12:06,110 ‫So as long as the orders queue count is greater than zero, we are going to execute this while loop 161 00:12:06,530 --> 00:12:11,040 ‫and we're going to remove the order at the front of the queue. 162 00:12:11,060 --> 00:12:16,820 ‫So the order that came in first, so to speak, and store it in a variable called current order. 163 00:12:17,120 --> 00:12:23,090 ‫So we have this order object which is going to be called Current Order, which will basically be the 164 00:12:23,090 --> 00:12:27,380 ‫result of the queue in the first item of our orders queue. 165 00:12:27,620 --> 00:12:35,570 ‫So basically taking away this item first, then this item, then that item, then this item, then that 166 00:12:35,570 --> 00:12:36,560 ‫and then that one. 167 00:12:37,490 --> 00:12:42,800 ‫And then it will process each and every one of them step by step. 168 00:12:43,130 --> 00:12:47,270 ‫So processing and our case will just be something very simple. 169 00:12:47,280 --> 00:12:49,770 ‫It will just print it onto the console. 170 00:12:49,790 --> 00:12:54,830 ‫But of course, you could run something way more complex in the background here. 171 00:12:55,130 --> 00:13:01,250 ‫This could trigger some events sending out an email, for example, to an employee who has time to take 172 00:13:01,250 --> 00:13:07,850 ‫care of it or whatever processing system you have employed in your company. 173 00:13:08,570 --> 00:13:09,230 ‫All right. 174 00:13:09,380 --> 00:13:11,270 ‫And yeah, that's basically it. 175 00:13:11,300 --> 00:13:14,780 ‫So now let's run this application to see the results. 176 00:13:14,780 --> 00:13:16,700 ‫And we can see order one was processed. 177 00:13:16,700 --> 00:13:22,240 ‫Order two was processed, then order six, then three, four, five. 178 00:13:22,730 --> 00:13:24,860 ‫So we are processing the orders. 179 00:13:25,250 --> 00:13:29,630 ‫Of course, in the order that we queued them with that we end queue them. 180 00:13:29,630 --> 00:13:34,070 ‫You can see here we are in queuing from branch 1/1 and then from branch two. 181 00:13:34,430 --> 00:13:41,090 ‫So just imagine for the following scenario where the orders that are created in the system internally, 182 00:13:41,090 --> 00:13:49,820 ‫but they first have to be accepted and fully pushed to the processing entity, whatever that entity 183 00:13:49,820 --> 00:13:53,840 ‫is, like the another office or so, for example. 184 00:13:53,840 --> 00:13:56,570 ‫And they do it at the end of the day, for example. 185 00:13:56,570 --> 00:14:03,800 ‫So first branch one finishes their stuff and they say, okay, well we accept those orders. 186 00:14:03,800 --> 00:14:08,350 ‫And then later that day, because let's say they are in a different time zone. 187 00:14:08,360 --> 00:14:11,990 ‫For example, Branch two says, Yeah, we are agreeing. 188 00:14:11,990 --> 00:14:17,870 ‫Well, we send out our orders as approved at that point as well, and then they are started to being 189 00:14:17,870 --> 00:14:18,680 ‫processed. 190 00:14:19,160 --> 00:14:23,360 ‫So that's how this example could be considered, for example, right? 191 00:14:23,390 --> 00:14:31,310 ‫Otherwise, of course, you would order them straight away based on their ID or based on the time that 192 00:14:31,310 --> 00:14:32,990 ‫they were created. 193 00:14:34,190 --> 00:14:34,850 ‫All right. 194 00:14:34,850 --> 00:14:41,850 ‫So now you have a certain idea of how these orders work and how the queues work. 195 00:14:41,870 --> 00:14:43,280 ‫That's the more important part. 196 00:14:43,280 --> 00:14:48,380 ‫So now you can create your own queues and use them in your applications. 197 00:14:48,950 --> 00:14:49,340 ‫All right. 198 00:14:49,340 --> 00:14:50,810 ‫So I hope you enjoyed this video. 199 00:14:50,840 --> 00:14:51,950 ‫See you in the next one.