mergeIfNotContained

fun <T> MutableCollection<T>.mergeIfNotContained(collectionToMerge: Collection<T>)

Method used to merge items from the collectionToMerge collection that are not present in the main collection. For example:

val mainCollection = mutableListOf(1, 2, 3)

val collectionToMerge = listOf(2, 3, 4)

// apply the retainAndAdd method
mainCollection.mergeIfNotContained(
  collectionToMerge = collectionToMerge
)

println(mainCollection) // the result printed will be 1, 2, 3, 4

The duplicate values will be considered as one element, so will be merged