How can I use Stack (from java) in Kotlin?
Or theres any other alternative?
- I'm trying to convert list to Stack
How can I use Stack (from java) in Kotlin?
Or theres any other alternative?
Kotlin 1.3.70 introduced the kotlin.collections.ArrayDeque
class, which functions as both a queue and a stack, like Java's java.util.Deque
(Deque meaning "double-ended queue"). It was created out of a necessity for a multiplatform ArrayDeque
implementation.
val stack = ArrayDeque(listOf(1, 2, 3)) // stack: [1, 2, 3]
stack.addLast(0) // stack: [1, 2, 3, 0] (push)
val value = stack.removeLast() // value: 0, stack: [1, 2, 3] (pop)
Note that if an ArrayDeque
is empty when you call removeFirst
or removeLast
, it will throw a kotlin.NoSuchElementException
. If you don't want to check the size of your deque every time you need to access it, then you should use the removeFirstOrNull
and removeLastOrNull
functions.
Optional Snippets
ArrayDeque
constructor function:
inline fun <T> arrayDequeOf(vararg elements: T) = ArrayDeque(elements.toList())
// ...
val stack = arrayDequeOf(1, 2, 3)
Stack
-like ArrayDeque
calls:
inline fun <T> ArrayDeque<T>.push(element: T) = addLast(element) // returns Unit
inline fun <T> ArrayDeque<T>.pop() = removeLastOrNull() // returns T?
import java.util.ArrayDeque
var stack = ArrayDeque<Int>()
stack.push(1)
stack.push(2)
stack.push(3)
stack.push(4)
println(stack) // --> [4, 3, 2, 1]
println(stack.isEmpty()) // --> false
println(stack.peek()) // --> 4
println(stack) // --> [4, 3, 2, 1]
println(stack.pop()) // --> 4
println(stack) // --> [3, 2, 1]
stack.push(9)
println(stack) // --> [9, 3, 2, 1]
You can use following:
/**
* Stack as type alias of Mutable List
*/
typealias Stack<T> = MutableList<T>
/**
* Pushes item to [Stack]
* @param item Item to be pushed
*/
inline fun <T> Stack<T>.push(item: T) = add(item)
/**
* Pops (removes and return) last item from [Stack]
* @return item Last item if [Stack] is not empty, null otherwise
*/
fun <T> Stack<T>.pop(): T? = if (isNotEmpty()) removeAt(lastIndex) else null
/**
* Peeks (return) last item from [Stack]
* @return item Last item if [Stack] is not empty, null otherwise
*/
fun <T> Stack<T>.peek(): T? = if (isNotEmpty()) this[lastIndex] else null
This is done in the same way as you would in Java, but with Kotlin syntax - notably different are the val
keyword and lack of new keyword. For example:
import java.util.Stack
...
val someList = ArrayList()
...
val stack = Stack()
stack.addAll(someList)
This is a few years old but I suspect there's room for a different approach. If you want to use a stack structure in Kotlin you certainly don't need to resort to Java. You could easily just create a new class with an internal Kotlin list and stack-like public functions, or use Kotlin's extension methods to give an existing Kotlin collection "stack-like" functionality, for example:
fun <T> MutableList<T>.push(item: T) = this.add(this.count(), item)
fun <T> MutableList<T>.pop(): T? = if(this.count() > 0) this.removeAt(this.count() - 1) else null
fun <T> MutableList<T>.peek(): T? = if(this.count() > 0) this[this.count() - 1] else null
fun <T> MutableList<T>.hasMore() = this.count() > 0
Then, optionally, you could use a typealias to make it more obvious what you're trying to do when using those functions:
typealias Stack = MutableList<MyClass>
Then create one and use it:
val myStack: Stack = mutableListOf()
myStack.push(MyClass())
myStack.pop()
etc
I don't believe there's a specific separate implementation of Stack in Kotlin. You could definitely use the answer by Ed.
Alternatively, you could use a mutableListOf<DataType>
construct, and then have custom methods that are on top of this.
It would be something like this :
var stackDemo = mutableListOf<String>()
To push an element
var count = stackDemo.count()
stackDemo.add(count,"One")
To pop an element
var count = stackDemo.count()
stackDemo.removeAt(count)
You can refer to this Github link for a model implementation
you can define Stack like it.
val stack = Stack<YourStackType>()
notice that set the data type of your stack, for example stack of Int is like this:
val stack = Stack<Int>()
after that you can use push , pop , or other stack operations
example for Int stack :
a:Int = 10
stack.push(a)
a = stack.pop()