https://kotlinlang.org logo
Title
n

neil.armstrong

03/13/2018, 11:36 AM
listOf
will create an immutable list
/**
 * Returns an immutable list containing only the specified object [element].
 * The returned list is serializable.
 * @sample samples.collections.Collections.Lists.singletonReadOnlyList
 */
@JvmVersion
public fun <T> listOf(element: T): List<T> = java.util.Collections.singletonList(element)
a

Andreas Sinz

03/13/2018, 11:49 AM
@nickk @neil.armstrong This is only true for Lists with a single element, if your list has more items you end up with a read-only list:
/**
 * Returns a new read-only list of given elements.  The returned list is serializable (JVM).
 * @sample samples.collections.Collections.Lists.readOnlyList
 */
public fun <T> listOf(vararg elements: T): List<T> = ...
n

neil.armstrong

03/13/2018, 11:51 AM
Ah yes, I just linked the first one I saw 😄
a

Andreas Sinz

03/13/2018, 11:53 AM
and sadly its the same for
list1 + list2
, it returns `ArrayList`:
/**
 * Returns a list containing all elements of the original collection and then all elements of the given [elements] collection.
 */
public operator fun <T> Collection<T>.plus(elements: Iterable<T>): List<T> {
    if (elements is Collection) {
        val result = ArrayList<T>(this.size + elements.size)
        result.addAll(this)
        result.addAll(elements)
        return result
    } else {
        val result = ArrayList<T>(this)
        result.addAll(elements)
        return result
    }
}