Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create List from Set in Kotlin?

Suppose I have a set with elements. How to create List of the same elements? I see methiods asSequence and asIterable, but no asList, why?

like image 803
Dims Avatar asked Mar 11 '18 21:03

Dims


People also ask

How do I add a list on Kotlin?

To add a single element to a list or a set, use the add() function. The specified object is appended to the end of the collection. addAll() adds every element of the argument object to a list or a set. The argument can be an Iterable , a Sequence , or an Array .

How do I make a list of string in Kotlin?

To define a list of Strings in Kotlin, call listOf() function and pass all the Strings as arguments to it. listOf() function returns a read-only List. Since, we are passing strings for elements parameter, listOf() function returns a List<String> object.

How do I create an integer list in Kotlin?

To define a list of integers in Kotlin, call listOf() function and pass all the integers as arguments to it. listOf() function returns a read-only List. Since, we are passing integers for elements parameter, listOf() function returns a List<Int> object.


Video Answer


1 Answers

The function you're looking for is called toList():

val set: Set<Int> = setOf(1,2,3)
val list: List<Int> = set.toList()
like image 57
s1m0nw1 Avatar answered Oct 29 '22 21:10

s1m0nw1