Reputation: 3831
I am new to kotlin and I am facing this issue
I have an ArrayList<String?>
and I have to pass it to a function that accepts List<CharSequence>
I tried to find a way to convert them but couldnt, How can I convert ArrayList<String?>
to List<CharSequence>
in kotlin
Upvotes: 1
Views: 951
Reputation: 405
CharSequence
is an interface and String
class is already implemented that. Hence cast to CharSequence
should be enough. map
function returns List<T>
val list = arrayList.map { it as CharSequence }
With filtering null values
val list: List<CharSequence> = arrayList.filterNotNull()
Upvotes: 0
Reputation: 93609
Use
val list: List<CharSequence> = arrayList.filterNotNull()
filterNotNull()
removes any possible null values so you have a List<String>
, which can be automatically up-cast to a List<CharSequence>
.
Upvotes: 4