Kotlin merge two nullable mutable list

kotlin merge two lists by id
kotlin merge two lists without duplicates
kotlin mutable list example
kotlin combine multiple lists
kotlin mutable list to list
kotlin flatten list of maps
kotlin map two lists
kotlin list of lists
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.

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()
    

[SOLVED] Kotlin merge two nullable mutable list, Here are couple of solutions. In case if you need to add all elements to mutableList1 : val mutableList1: MutableList<Any?>? MutableList class is used to create mutable lists in which the elements can be added or removed. The method mutableListOf() returns an instance of MutableList Interface and takes the array of a particular type or mixed (depends on the type of MutableList instance) elements or it can be null also.

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.

Kotlin merge two nullable mutable list, val mutableList2: MutableList<TeamInvitationData?>? addAll method can be use to merge nullable mutable list but, here it throws me compile time error. Update or replace operations only work with Kotlin mutable List. The examples show you how to: update/replace item in mutable List at specified index using set() method or operator [] replace all items in mutable List with given value using fill() method. replace each item in the list with a result of a function using replaceAll() method.

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

MutableList, contained in the list. The mutable list is invariant on its element type. Returns true if this nullable collection is either null or empty. fun <T> Collection<T>? Shared Mutable State and Concurrency. Null Safety. Exceptions. Annotations. Kotlin Trademark is protected under the Kotlin Foundation.

flatten, Returns a single list of all elements from all arrays in the given array. import kotlin .test.* fun main(args: Array<String>) { //sampleStart val deepArray = arrayOf(� Searches this list or its range for an element having the key returned by the specified selector function equal to the provided key value using the binary search algorithm. The list is expected to be sorted into ascending order according to the Comparable natural ordering of keys of its elements. otherwise the result is undefined.

union, List Specific Operations � Set Specific Operations � Map Specific Operations Shared Mutable State and Concurrency � Select Expression (experimental). In this recipe, we will see how to merge two or more collections into one. However, before we move ahead, we need to understand the difference between mutable and immutable types. An immutable type object is an object that cannot be changed. For example, if we define an immutable list, we won't be able to add other objects to it.

mutableListOf, println(list) // [1, 2, 3]. Common. JVM. JS. Native. 1.0. fun <T> mutableListOf( vararg elements: T): MutableList<T>. Returns a new MutableList with the given� Creates a new mutable list with the specified size, where each element is calculated by calling the specified init function. The function init is called for each list element sequentially starting from the first one. It should return the value for a list element given its index.

Comments
  • On what line did the error occur? Why don't you show the code you actually wrote?
  • this one have same code actually I did and unbox from the other element.
  • You have shown no code that attempts to use addAll. You don't show any line of code that would need type inference, either.
  • You pasted two snippets that don't combine into a whole. My best guess is that you expect map1 and map2 to be MutableList because listOne and listTwo are mutable, but map actually returns an immutable List and so you can't call addAll on it. While your question is about merging mutable lists, your code doesn't attempt that.
  • Is it necessary to use inner let ? but I like 4th one.
  • @QuokMoon you can replace inner let with if (list != null) ... else null but it will look massive comparing to ?.let
  • Against of null , Let seem perfect.
  • My Java intuition betrayed me on the last example, i didn't realize you can declare extensions on nullable types.