The Deque interface extends the Queue interface and allows for items to be added and removed from both ends of the Deque. In other words, we are not only able to make FIFO (first in, first out) structures, but we may also make LIFO structures (last-in, first-out). One real-world use case for LIFO may be an accounting program where inventory is tracked on a LIFO basis to reduce a company’s cost. If we were writing a Kotlin application for such a task, we would use Deque.
The Deque has a variety of methods that are commonly used. Here is a list of the methods followed by a brief description.
Creating a Deque
The LinkedList class in an implementation of the Deque.
val deque : Deque<String> = LinkedList()
Operations with Excpetions
addFirst(T) : void
The addFirst method adds an item to the front of the Deque. The method can raise an exception if the operation fails.
deque.addFirst("Bob Belcher")
addLast(T) : void
The addLast method add an item to the end of the Deque. The method can raise an exception if the operation fails.
deque.addLast("Linda Belcher")
removeFirst() : T
The removeFirst() method removes the first item from the Deque and return it. A NoSuchElement exception is thrown if the Deque is empty.
val bob = deque.removeFirst()
removeLast() : T
The removeLast() method removes the last item from the Deque and returns it. A NoSuchElement exception is thrown if the Deque is empty.
val linda = deque.removeLast()
getFirst() : T
The getFirst() method returns the first item from the Deque, but it does not remove it from the Deque. A NoSuchElement exception is thrown if the Deque is empty.
val bob = deque.getFirst(); println( deque.contains("Bob") ) ///prints True
getLast() : T
The getLast() method returns the last item from the Deque, but it does not remove it from the deque. A NoSuchElement exception is thrown if the Deque is empty.
val linda = deque.getLast() println( deque.contains("Linda") ) //prints True
Methods that Do Not Throw Exceptions
offerFirst(T) : Boolean
Attempts to add the item to the front of the Deque and returns true if successful otherwise false.
deque.offerFirst("Gene")
offerLast(T) : Boolean
Attempts to add the item to the end of the Deque and returns true if successful otherwise false.
deque.offerLast("Tina")
pollFirst() : T?
Removes and returns an element from the front of the Deque. It will return null if the operation fails.
val gene = deque.pollFirst()
pollLast() : T?
Removes and returns an element from the end of the Deque. It will return null if the operation fails.
val tina = deque.pollLast()
peekFirst() : T?
Returns either the element or null from the front of the Deque but does not remove it from the Deque.
val gene = deque.peekFirst() println( deque.contains("Gene") )//prints true
peekLast() : T?
Returns either the element or null from the end of the Deque but does not remove it from the Deque.
val tina = deque.peekLast() println( deque.contains("Tina") )//prints true
Putting it Together
Below is an example program that uses a Deque as a stack (a LIFO datastructure). It simulates layers of soils where the top most layer has to be dug up first prior to digging up older layers of soils.
import java.util.* fun main(args : Array<String>){ val soils : Deque<String> = LinkedList() with (soils){ add("Bedrock") add("Clay") add("Sand") add("Top Soil") } while(soils.isNotEmpty()){ println("Digging up layer => " + soils.removeLast()) } }
Output
Digging up layer => Top Soil Digging up layer => Sand Digging up layer => Clay Digging up layer => Bedrock