Kotlin Deque
$count++; if($count == 1) { include "../mobilemenu.php"; } if ($count == 2) { include "../sharemediasubfolder.php"; } ?>
As a concept, Deque provides, a two way First in - First Out data structure. Meaning we can add elements from any end of the queue, beginning or end, and we can remove elements from any end of the Queue, beginning or the end.
So, a Deque can act both as a Queue and a Stack.
Deque is an interface defined in java.util.Deque.
Deque implementation is provided by following classes -
Creating a Deque
Consider a Deque of planets as shown below. We are creating a LinkedList with three planets to begin with.
package com.cosmic.learn
import java.util.LinkedList
fun main(args: Array<String>)
{
var planetsDeque = LinkedList<String>();
planetsDeque.addAll(listOf("Mercury", "Venus", "Earth"))
for(planet in planetsDeque)
{
println(planet)
}
}
Output:
Mercury
Venus
Earth
We can add element to the end of a Deque, using addLast method or add method. In the following example, we used addLast method, to add Jupiter to the end of our Deque.
package com.cosmic.learn
import java.util.LinkedList
fun main(args: Array<String>)
{
var planetsDeque = LinkedList<String>();
planetsDeque.addAll(listOf("Mercury", "Venus", "Earth"))
planetsDeque.addLast("Jupiter")
for(planet in planetsDeque)
{
println("Planet = $planet")
}
}
Output:
Planet = Mercury
Planet = Venus
Planet = Earth
Planet = Jupiter
Add element to front of Deque
We can add element to the end of a Deque, using addFirst method. In the following example, we used addFirst method, to add Jupiter to the front of our Deque.
package com.cosmic.learn
import java.util.LinkedList
fun main(args: Array<String>)
{
var planetsDeque = LinkedList<String>();
planetsDeque.addAll(listOf("Mercury", "Venus", "Earth"))
planetsDeque.addFirst("Jupiter")
for(planet in planetsDeque)
{
println("Planet = $planet")
}
}
Output:
Planet = Jupiter
Planet = Mercury
Planet = Venus
Planet = Earth
Remove first element from Deque
We can remove and get the First element from the Deque using removeFirst method or remove method. In the following example, we used removeFirst method to remove and get the elements from the Deque until the Deque is empty. We can check if Deque is empty or not using the isEmpty method.
package com.cosmic.learn
import java.util.LinkedList
fun main(args: Array<String>)
{
var planetsDeque = LinkedList<String>();
planetsDeque.addAll(listOf("Mercury", "Venus", "Earth"))
planetsDeque.addLast("Jupiter")
planetsDeque.addLast("Neptune")
while(!planetsDeque.isEmpty())
{
println(planetsDeque.removeFirst());
}
}
Output:
Mercury
Venus
Earth
Jupiter
Neptune
We can remove and get the last element from the Deque using removeLast method. In the following example, we used removeLast method to remove and get the elements from the Deque until the Deque is empty. We can check if Deque is empty or not using the isEmpty method.
package com.cosmic.learn
import java.util.LinkedList
fun main(args: Array<String>)
{
var planetsDeque = LinkedList<String>();
planetsDeque.addAll(listOf("Mercury", "Venus", "Earth"))
planetsDeque.addLast("Jupiter")
planetsDeque.addLast("Neptune")
while(!planetsDeque.isEmpty())
{
println(planetsDeque.removeLast());
}
}
Output:
Neptune
Jupiter
Earth
Venus
Mercury
Instead of removing an element, if we want to just see the first element in the Deque, for such a use case, we can use first property. First property on the Deque returns us the first element in the Deque. In the following example, we are accessing first property to find out the first element in the Deque.
package com.cosmic.learn
import java.util.LinkedList
fun main(args: Array<String>)
{
var planetsDeque = LinkedList<String>();
planetsDeque.addAll(listOf("Mercury", "Venus", "Earth"))
println("First in Deque: ")
println(planetsDeque.first);
println("Removing first in Deque...")
planetsDeque.removeFirst();
println("New first in Deque: ")
println(planetsDeque.first);
}
Output:
First in Deque:
Mercury
Removing first in Deque...
New first in Deque:
Venus
Instead of removing an element, if we want to just see the last element in the Deque, for such a use case, we can use last property. Last property on the Deque returns us the last element in the Deque. In the following example, we are accessing last property to find out the last element in the Deque.
package com.cosmic.learn
import java.util.LinkedList
fun main(args: Array<String>)
{
var planetsDeque = LinkedList<String>();
planetsDeque.addAll(listOf("Mercury", "Venus", "Earth"))
println("Last in Deque: ")
println(planetsDeque.last);
println("Removing last in Deque...")
planetsDeque.removeLast();
println("New last in Deque: ")
println(planetsDeque.last);
}
Output:
Last in Deque:
Earth
Removing last in Deque...
New last in Deque:
Venus
What happens if Deque is empty, and we attempt to access the first element? In that case, we will encounter java.util.NoSuchElementException. In the following example, we removed all the elements from the Deque and then tried to access the First element in the Deque.
package com.cosmic.learn
import java.util.LinkedList
fun main(args: Array<String>)
{
var planetsDeque = LinkedList<String>();
planetsDeque.addAll(listOf("Mercury", "Venus", "Earth"))
println("First in Deque: ")
println(planetsDeque.first);
println("Removing first in Queue...")
planetsDeque.removeFirst();
println("New first in Deque: ")
println(planetsDeque.first);
println("Removing first in Deque...")
planetsDeque.removeFirst()
println("New first in Deque: ")
println(planetsDeque.first)
println("Removing first in Deque...")
planetsDeque.removeFirst()
println("New first in Deque: ")
println(planetsDeque.first)
}
Output:
First in Deque:
Mercury
Removing first in Deque...
New first in Deque:
Venus
Removing first in Deque...
New first in Deque:
Earth
Removing first in Deque...
New first in Deque:
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.LinkedList.getFirst(LinkedList.java:248)
at com.cosmic.learn.CosmicTestDriverKt.main(CosmicTestDriver.kt:29)
What happens if Deque is empty, and we attempt to access the first element? In that case, we will encounter java.util.NoSuchElementException. In the following example, we removed all the elements from the Deque and then tried to access the First element in the Deque.
package com.cosmic.learn
import java.util.LinkedList
fun main(args: Array<String>)
{
var planetsDeque = LinkedList<String>();
planetsDeque.addAll(listOf("Mercury", "Venus", "Earth"))
println("First in Deque: ")
println(planetsDeque.first);
println("Removing last in Queue...")
planetsDeque.removeLast();
println("New first in Deque: ")
println(planetsDeque.first);
println("Removing last in Deque...")
planetsDeque.removeLast()
println("New first in Deque: ")
println(planetsDeque.first)
println("Removing last in Deque...")
planetsDeque.removeLast()
println("New first in Deque: ")
println(planetsDeque.first)
}
Output:
First in Deque:
Mercury
Removing last in Queue...
New first in Deque:
Mercury
Removing last in Deque...
New first in Deque:
Mercury
Removing last in Deque...
New first in Deque:
Exception in thread "main" java.util.NoSuchElementException
at java.base/java.util.LinkedList.getFirst(LinkedList.java:248)
at com.cosmic.learn.CosmicTestDriverKt.main(CosmicTestDriver.kt:29)
To avoid the java.util.NoSuchElementException and crashing our program, we can use a try ... catch block we saw in the Exception Handling section of the tutorial. However, there is a much cleaner way. We can use the method, isEmpty() to check if the Deque is empty or not, before attempting to access the first element.
In the following example, we used isEmpty to check if the Deque is empty, before we tried to access the first element in the Deque.
package com.cosmic.learn
import java.util.LinkedList
fun main(args: Array<String>)
{
var planetsDeque = LinkedList<String>();
planetsDeque.addAll(listOf("Mercury", "Venus", "Earth"))
println("First in Deque: ")
println(planetsDeque.first);
println("Removing first in Deque...")
planetsDeque.removeFirst();
println("New first in Deque: ")
println(planetsDeque.first);
println("Removing first in Deque...")
planetsDeque.removeFirst()
println("New first in Deque: ")
println(planetsDeque.first)
println("Removing first in Deque...")
planetsDeque.removeFirst()
if(planetsDeque.isEmpty())
{
println("Deque is empty! Returning")
return
}
println("New first in Deque: ")
println(planetsDeque.first)
}
Output:
First in Deque:
Mercury
Removing first in Deque...
New first in Deque:
Venus
Removing first in Deque...
New first in Deque:
Earth
Removing first in Deque...
Deque is empty! Returning
Tries to add element to end of Deque while checking for capacity constraints. It returns false if adding element to the end of Deque failed. This is alternative to addLast, which will throw exception if the Deque capacity constraints are reached.
In the following example, we used offerLast function to add Jupiter to end of our Deque. We are also printing the result of offerLast function.
package com.cosmic.learn
import java.util.LinkedList
fun main(args: Array<String>)
{
var planetsDeque = LinkedList<String>();
planetsDeque.addAll(listOf("Mercury", "Venus", "Earth"))
val offerLast: Boolean = planetsDeque.offerLast("Jupiter")
println("Offer last result = $offerLast")
for(planet in planetsDeque)
{
println(planet)
}
}
Output:
Offer last result = true
Mercury
Venus
Earth
Jupiter
Tries to add element to start of Deque while checking for capacity constraints. It returns false if adding element to the beginning of Deque failed. This is alternative to addFirst, which will throw exception if the Deque capacity constraints are reached.
In the following example, we used offerFirst function to add Jupiter to beginning of our Deque. We are also printing the result of offerFirst function.
package com.cosmic.learn
import java.util.LinkedList
fun main(args: Array<String>)
{
var planetsDeque = LinkedList<String>();
planetsDeque.addAll(listOf("Mercury", "Venus", "Earth"))
val offerFirst: Boolean = planetsDeque.offerFirst("Jupiter")
println("Offer First result = $offerFirst")
for(planet in planetsDeque)
{
println(planet)
}
}
Output:
Offer First result = true
Jupiter
Mercury
Venus
Earth
pollFirst function Tries to remove the element from the Deque, and if it fails, returns null instead of exception. This is alternative to the removeFirst function, which throws Exception when the removal of element from the Deque failed.
In the following example, we used pollFirst function, to remove planets from the Deque. As you see the last result is null. This is because by that time, the Deque is empty and pollFirst has nothing to remove, it returns null.
package com.cosmic.learn
import java.util.LinkedList
fun main(args: Array<String>)
{
var planetsDeque = LinkedList<String>();
planetsDeque.addAll(listOf("Mercury", "Venus", "Earth"))
var planet = planetsDeque.pollFirst()
println(planet)
planet = planetsDeque.pollFirst()
println(planet)
planet = planetsDeque.pollFirst()
println(planet)
planet = planetsDeque.pollFirst()
println(planet)
}
Output:
Mercury
Venus
Earth
null
pollLast function Tries to remove the element from the end of Deque, and if it fails, returns null instead of exception. This is alternative to the removeLast function, which throws Exception when the removal of element from the Deque failed.
In the following example, we used pollLast function, to remove planets from the Deque. As you see the last result is null. This is because by that time, the Deque is empty and pollLast has nothing to remove, it returns null.
package com.cosmic.learn
import java.util.LinkedList
fun main(args: Array<String>)
{
var planetsDeque = LinkedList<String>();
planetsDeque.addAll(listOf("Mercury", "Venus", "Earth"))
var planet = planetsDeque.pollLast()
println(planet)
planet = planetsDeque.pollLast()
println(planet)
planet = planetsDeque.pollLast()
println(planet)
planet = planetsDeque.pollLast()
println(planet)
}
Output:
Earth
Venus
Mercury
null
peekFirst or peek function tries to get the first element from the Deque, and if it fails, returns null instead of exception. This is alternative to the getFirst function / first property, which throws Exception when the retrieval of element from the Deque failed.
In the following example, we used peekFirst function, to get first planet from the Deque. As you see the last result is null. This is because by that time, the Deque is empty and peekFirst has nothing to retrieve, it returns null.
package com.cosmic.learn
import java.util.LinkedList
fun main(args: Array<String>)
{
var planetsDeque = LinkedList<String>();
planetsDeque.addAll(listOf("Mercury", "Venus", "Earth"))
var planet = planetsDeque.peekFirst()
println("Peekfirst $planet")
planet = planetsDeque.pollFirst()
println("Removed $planet")
planet = planetsDeque.pollFirst()
println("Removed $planet")
planet = planetsDeque.pollFirst()
println("Removed $planet")
planet = planetsDeque.peekFirst()
println("PeekFirst $planet")
}
Output:
Peekfirst Mercury
Removed Mercury
Removed Venus
Removed Earth
PeekFirst null
peekLast function tries to get the last element from the Deque, and if it fails, returns null instead of exception. This is alternative to the getLast function / last property, which throws Exception when the retrieval of element from the Deque failed.
In the following example, we used peekLast function, to get last planet from the Deque. As you see the last result is null. This is because by that time, the Deque is empty and peekLast has nothing to retrieve, it returns null.
package com.cosmic.learn
import java.util.LinkedList
fun main(args: Array<String>)
{
var planetsDeque = LinkedList<String>();
planetsDeque.addAll(listOf("Mercury", "Venus", "Earth"))
var planet = planetsDeque.peekLast()
println("Peeklast $planet")
planet = planetsDeque.pollLast()
println("Removed $planet")
planet = planetsDeque.pollLast()
println("Removed $planet")
planet = planetsDeque.pollLast()
println("Removed $planet")
planet = planetsDeque.peekLast()
println("Peeklast $planet")
}
Output:
Peeklast Earth
Removed Earth
Removed Venus
Removed Mercury
Peeklast null