Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin merge two nullable mutable list

val mutableList1: MutableList<TeamInvitationData?>?
val mutableList2: MutableList<TeamInvitationData?>?

addAll method can be use to merge nullable mutable list but, here it throws me compile time error.

Example:

val map1 = listOne?.map { TeamInvitationData(it) }
val map2 = listTwo?.map { TeamInvitationData(it) }
map1.addAll(map2)

Type interface failed ,Please try to specify type argument explicitly.

Here Any way can I merge this two array , thanks in advance.

like image 841
QuokMoon Avatar asked Jan 12 '18 11:01

QuokMoon


4 Answers

Here are couple of solutions.

  1. In case if you need to add all elements to mutableList1:

    val mutableList1: MutableList<Any?>? = ...
    val mutableList2: MutableList<Any?>? = ...
    
    mutableList1?.let { list1 -> mutableList2?.let(list1::addAll) }
    
  2. In case if you need new nullable list as result:

    val mutableList1: MutableList<Any?>? = ...
    val mutableList2: MutableList<Any?>? = ...
    
    val list3: List<Any?>? = mutableList1?.let { list1 ->
        mutableList2?.let { list2 -> list1 + list2 }
    }
    
  3. In case if you need new nullable mutable list as result:

    val mutableList1: MutableList<Any?>? = ...
    val mutableList2: MutableList<Any?>? = ...
    
    val list3: MutableList<Any?>? = mutableList1
            ?.let { list1 -> mutableList2?.let { list2 -> list1 + list2 } }
            ?.toMutableList()
    
  4. In case if you need new non-null list as result:

    val mutableList1: MutableList<Any?>? = ...
    val mutableList2: MutableList<Any?>? = ...
    
    val list3: List<Any?> = mutableList1.orEmpty() + mutableList2.orEmpty()
    
like image 51
hluhovskyi Avatar answered Nov 14 '22 14:11

hluhovskyi


plus. Returns an List containing all elements of the original collection and then the given Iterable. source

Collection<T>.plus(elements: Iterable<T>): List<T>

Another Good read here

like image 38
Ayush Jain Avatar answered Nov 14 '22 14:11

Ayush Jain


Based on your snippets, which don't make a consistent whole, I made some guesses as to what you actually wanted to achieve:

val mutableList1: MutableList<String?>? = ...
val mutableList2: MutableList<String?>? = ...

val mapped1 = mutableList1?.mapTo(ArrayList()) { TeamInvitationData(it) }
val mapped2 = mutableList2?.mapTo(ArrayList()) { TeamInvitationData(it) }

mapped1?.addAll(mapped2.orEmpty())

The key point to note is that map() returns an immutable list regardless of the type of the input list. To get a mutable list you must use mapTo(destination) { ... }. Once that is fixed, you can use addAll() as shown in the last line.

like image 1
Marko Topolnik Avatar answered Nov 14 '22 12:11

Marko Topolnik


You can use java ArrayList to add all items sample bellow

val list: MutableList<Student> = ArrayList()

list.addAll(otherListOfStudent)

Happy Coding :)

like image 1
Neeraj Singh Avatar answered Nov 14 '22 12:11

Neeraj Singh