2️⃣

17.2 Queues

17.2 Queues

image

In the above picture, we observe that elements are first pushed to the back of the queue, and then the elements are removed through the front. Java’s implementation of queue also supports array operations including get() and indexOf(). This however means that insertion and eviction operations are much slower than if implemented using a simple linked list. For instance, C++ offers significant advantages over Java’s implementation, but Java’s implementation also has its advantages.

Regardless, below is a brief overview of the methods used in Java:

image

We observe here that the argument type is called E which is unique to Java, employing something called Java Generics. I will not talk too much about this further, but it is somewhat similar to C++’s template parameters (and for those who are curious, templates is one of the most powerful tools in C++, allowing users to do some really crazy stuff like partial template specialization). What this allows the user to do is define the type of objects which the queue contains. For now, pretend that E is replaced by the Integer type in the Java SE library.'

Previous Section

Next Section

⚖️

Copyright © 2021 Code 4 Tomorrow. All rights reserved. The code in this course is licensed under the MIT License. If you would like to use content from any of our courses, you must obtain our explicit written permission and provide credit. Please contact classes@code4tomorrow.org for inquiries.