In Kotlin, are data structures such as stack, queue, heap, tree, etc. supported through standard library?
As a person mostly familiar with Python but didn't use Java before jumping into Kotlin, this question came to my mind.
For example, if one wants to use Kotlin for competitive programming, or if one doesn't want to reinvent the wheel implementing the common data structures, are these supported through the standard library? Or are they not supported due to some design reason of Kotlin? At least I wasn't able to find these in the documentation. Then if one is using Kotlin on JVM, do people usually use the implementations from Java's library?
Kotlin ResourcesStack provides a data structure that implements a Last In First out mechanism on the objects on stack. This data structure is useful to implement a series function calls for instance. This is where StackOverflow exception comes from. Stack is part of the Java collections framework.
Kotlin Data structures and Algorithms enables you to write code that runs faster which is hugely important in web and mobile world. This book takes you through a pragmatic approach with techniques and real-world examples that you can use in your regular production environment.
Stacks are used prominently in all disciplines of programming, such as: Android uses the fragment stack to push and pop fragments into and out of an Activity. Memory allocation uses stacks at the architectural level. Memory for local variables is also managed using a stack.
What is a Kotlin queue? A queue is a type of interface collection in Kotlin that enables you to structure your data in a more efficient manner based on your chosen preference. The most popular implementation of the queue interface is the LinkedList , while others include ArrayDeque and PriorityQueue .
Kotlin mostly reuses Java's Collection API in this case, so you can utilize it freely. Collection API has 4 major interfaces:
Most of the needs can be satisfied with this interfaces' implementations.
Such as LinkedList
as a classic implementation in terms of Doubly Linked List data structure; also it implements Deque
. ArrayList
implements List
interface using array internally; it has much faster elements access due to arrays' indexed access efficiency nature (except of the case when you are accessing either first or last elements, because than it's identical), but insertions can really cost you on insertions not in the end or on insertions when the capacity of the internal array is exceeded, because it then should get reinitialized and refilled, which is a hit for both time and space. Java's internal System#arraycopy
is internally relatively fast though.
Stack and Queue both can be also fulfilled with ArrayDeque
, which is, again, an array-backed implementation, but in this case of Deque
.
TreeMap
uses Red-Black tree algorithm internally; TreeSet
uses TreeMap
internally is you need single elements rather than associations.
For hashtable-based implementations you can use HashMap
for associations and HashSet
for sole elements respectively (same story with reuse as for tree-based implementations).
For all the other things (thread-safe too - designed for concurrency or simple locking-applying decorators) you can search for an appropriate implementations of these interfaces actually - the standard library is big enough.
Adding to the above answer, Java's implementation of Heap is PriorityQueue, passing a comparator appropriately, and you can use it in Kotlin no problem.
Docs: https://docs.oracle.com/javase/7/docs/api/java/util/PriorityQueue.html
Check more: https://www.geeksforgeeks.org/priority-queue-class-in-java-2/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With